Android打開(kāi)相冊(cè)選擇單張圖片實(shí)現(xiàn)代碼

Android 打開(kāi)相冊(cè)選擇單張圖片實(shí)現(xiàn)代碼

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),蘆溪網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:蘆溪等地區(qū)。蘆溪做網(wǎng)站價(jià)格咨詢:028-86922220

                以下就是如何在應(yīng)用中打開(kāi)圖庫(kù)并選擇一張圖片進(jìn)行應(yīng)用,代碼很簡(jiǎn)單,注釋很詳細(xì)。

實(shí)現(xiàn)代碼:

btn8.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
/*在Activity Action里面有一個(gè)“ACTION_GET_CONTENT”字符串常量,
// 該常量讓用戶選擇特定類型的數(shù)據(jù),并返回該數(shù)據(jù)的URI.我們利用該常量,
//然后設(shè)置類型為“image/*”,就可獲得Android手機(jī)內(nèi)的所有image。*/
        Intent intent = new Intent();
        /* 開(kāi)啟Pictures畫(huà)面Type設(shè)定為image */
        intent.setType("image/*");
        /* 使用Intent.ACTION_GET_CONTENT這個(gè)Action */
        intent.setAction(Intent.ACTION_GET_CONTENT);
        /* 取得相片后返回本畫(huà)面 */
        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));
        /* 將Bitmap設(shè)定到ImageView */
        iv8.setImageBitmap(bitmap);
      } catch (FileNotFoundException e) {
        Log.e("Exception", e.getMessage(), e);
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

文章標(biāo)題:Android打開(kāi)相冊(cè)選擇單張圖片實(shí)現(xiàn)代碼
當(dāng)前鏈接:http://muchs.cn/article10/gpjpgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃外貿(mào)建站、定制開(kāi)發(fā)App開(kāi)發(fā)、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司