Android 從Gallery擷取圖片,

來源:互聯網
上載者:User

Android 從Gallery擷取圖片,

本文主要介紹Android中從Gallery擷取圖片

設計項目布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="selectImage"        android:text="選擇一張照片" />    <ImageView        android:id="@+id/iv"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></LinearLayout>

開啟packages\apps\Gallery下的資訊清單檔,可以看到其中包含下面的代碼:

 <activity android:name="com.android.camera.ImageGallery"                android:label="@string/gallery_label"                android:configChanges="orientation|keyboardHidden"                android:icon="@drawable/ic_launcher_gallery">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.VIEW" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.dir/image" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.VIEW" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.dir/video" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.GET_CONTENT" />                <category android:name="android.intent.category.OPENABLE" />                <data android:mimeType="vnd.android.cursor.dir/image" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.GET_CONTENT" />                <category android:name="android.intent.category.OPENABLE" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="image/*" />                <data android:mimeType="video/*" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.PICK" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="image/*" />                <data android:mimeType="video/*" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.PICK" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.dir/image" />            </intent-filter>        </activity>

邏輯部分代碼如下:

public class MainActivity extends Activity {    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv = (ImageView) findViewById(R.id.iv);    }    public void selectImage(View view) {        // 啟用系統圖庫的應用 選擇一張照片        Intent intent = new Intent();        intent.setAction(intent.ACTION_PICK);        intent.setType("image/*");        startActivityForResult(intent, 0);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (data != null) {            Uri uri = data.getData(); // 圖片的uri路徑            iv.setImageURI(uri);        }        super.onActivityResult(requestCode, resultCode, data);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.