android 利用隱式Intent開啟圖片

來源:互聯網
上載者:User

標籤:not   str   儲存   ted   子類   exp   mount   sd卡許可權   擷取   

實現功能

??點擊“查看圖片”時能夠跳出提示,選擇系統圖庫開啟還是自己編寫的應用開啟,並且對於下載好的圖片也有效。

  

1.我將 qiaoba.jpg 放在 res/drawable 目錄下,通過按鈕設定開啟圖片,在按鈕的監聽設定如下:

 findViewById(R.id.btn_openImage).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //要是自訂應用開啟圖片必須添加ACTION_VIEW的Intent                Intent intent = new Intent(Intent.ACTION_VIEW);                //進行圖片-->bitmap-->uri轉換                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.qiaoba);                //系統提供了現成的API讓使用者可以將自己喜歡的圖片儲存到系統相簿中.                String uriString = MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,null,null);                System.out.println(uriString);                Uri uri = Uri.parse(uriString);                //設定資料和類型可以用setData( )或setType( ) ,但是同時設定的話是不生效的,                // 只能使用setDataAndType( ) ,image代表圖片,星號代表圖片中所有格式,                // 可根據自己需要篩選,如只想開啟jpg類型圖片可用“image/jpg”                intent.setDataAndType(uri,"image/*");                startActivity(intent);            }        });

2.之前發送了開啟圖片的請求,接下來添加一個顯示圖片的java檔案“ShowImageAty.java”,並添加一個對應的“aty_show_image.xml”檔案

public class ShowImageAty extends AppCompatActivity {    ImageView ivQiaoba;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.aty_show_image);        ivQiaoba = (ImageView) findViewById(R.id.iv_qiaoba);        Intent getImage = getIntent();        //不為空白判斷        if (getImage != null) {            //擷取intent傳遞過來的uri資料            Uri data = getImage.getData();            if (data != null) {                ivQiaoba.setImageURI(data);            }        }    }}

3.在AndroidManifest.xml檔案中設定intent-filter標籤以及註冊網路以及檔案讀寫權限

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.jikexueyuan.notepad.simplepicturebrowser">    <!--訪問網路許可權 -->    <uses-permission android:name="android.permission.INTERNET" />    <!-- 讀寫SD卡許可權 -->    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".ShowImageAty"            android:exported="true">            <intent-filter>                <action android:name="android.intent.action.VIEW" />                <category android:name="android.intent.category.DEFAULT" />                <!-- 這個屬性用於設定資料的MIME類型,如:image/jpeg或audio/mpeg4-generic。               其子類型可用星號萬用字元(*)來代替,指示能夠跟任何子類型匹配。 -->                <data android:mimeType="image/*" />            </intent-filter>        </activity>    </application></manifest>

 

 

另外提一點,如果再android6.0,targetSDKVersion 23 以上運行就要手動註冊讀寫權限了,如果測試版本在 23 以上,會報這樣的錯

java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2317, uid=10064 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

 

 

手動註冊讀寫權限見:http://blog.csdn.net/dzsw0117/article/details/51212612

android 利用隱式Intent開啟圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.