天天看點

android 打開選擇圖檔大小,Android 打開相冊選擇單張圖檔實作代碼

Android 打開相冊選擇單張圖檔實作代碼

以下就是如何在應用中打開圖庫并選擇一張圖檔進行應用,代碼很簡單,注釋很詳細。

實作代碼:

btn8.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent intent = new Intent();

intent.setType("image

intent.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(intent, 1);

//Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

// startActivityForResult(intent, RESULT);

}

});

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK) {

Uri uri = data.getData();

Log.e("uri", uri.toString());

ContentResolver cr = this.getContentResolver();

try {

Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));

iv8.setImageBitmap(bitmap);

} catch (FileNotFoundException e) {

Log.e("Exception", e.getMessage(), e);

}

}

super.onActivityResult(requestCode, resultCode, data);

}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支援!