2017-08-06 13 views
0

我試圖通過意圖從圖庫或相機或文件管理器接收圖片。它傳遞了意圖成功但沒有收到圖片,但在圖庫的情況下,它正在接收選定的圖片。它傳遞給相機的意圖,但沒有收到點擊圖片,但是當我選擇圖庫選項時,它正在接收所選圖片

我的代碼:

private void choosePhotoFromGallery() { 
    Intent pickIntent = new Intent(); 
    pickIntent.setType("image/*"); 
    pickIntent.setAction(Intent.ACTION_GET_CONTENT); 
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    String pickTitle = "Take or select a photo"; 
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent }); 
    startActivityForResult(chooserIntent, 1);} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != Activity.RESULT_OK) { 
     return; 
    } 

    switch (requestCode) { 
     case 1: 
      try { 



InputStream inputStream = getContentResolve().openInputStream(data.getData()); 
       Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 

        imgView.setImageBitmap(scaledBitmap); 

      } catch (Exception e) { 
       Logger.log(e.getMessage()); 
      } 
      break; 

    } 
} 
+0

你採取了與相機有關的所有權限嗎?還有android M以後需要的運行時權限? –

+0

@kapsym我添加了權限,但仍未收到圖像 –

+0

我在下面添加了我的答案。看看並讓我知道你是否仍然面臨問題。 –

回答

0

與您的代碼的問題是這樣的:

data.getData() 

你需要從返回的意圖是這樣獲得額外的:

data.getExtras().get("data"); 

所以你的InputStream應該像 -

InputStream inputStream = getContentResolve().openInputStream(data.getExtras().get("data")); 

更新1:

您還可以修改你的方法,首先創建一個文件,然後將該文件URI傳遞到意圖做這種方式。這可以幫助您避免在以前的方法中可能遇到的設備特定問題。 -

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
// Ensure that there's a camera activity to handle the intent 
if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
          // Create the File where the photo should go 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
      } catch (IOException ex) { 
      ex.printStackTrace(); 
      } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 

      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
             Uri.fromFile(photoFile)); 
           startActivityForResult(takePictureIntent, 1734); 
     } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode==1734 && resultCode==RESULT_OK) 
     { 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 
     final int REQUIRED_SIZE = 200; 
     int scale = 1; 
     while (options.outWidth/scale/2 >= REQUIRED_SIZE 
       && options.outHeight/scale/2 >= REQUIRED_SIZE) 
      scale *= 2; 
     options.inSampleSize = scale; 
     options.inJustDecodeBounds = false; 
     Bitmap bitmap = BitmapFactory.decodeFile(path, options); 
    } 
} 

// This is just a method to create a File with current timestamp in name 
private File createImageFile() throws IOException { 
    // Create an image file name 
    File image=null; 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
     File storageDir = getExternalFilesDir(null); 
     if (!storageDir.exists()) { 
      storageDir.mkdir(); 
     } 
     image = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       storageDir  /* directory */ 
     ); 

     // Save a file: path for use with ACTION_VIEW intents 
     mCurrentPhotoPath = image.getAbsolutePath(); 
    } 
    return image; 

} 
+0

沒有發生這種變化 –

+0

您正在使用哪個設備進行測試? –

+0

@SrinivasNahak添加了一個更新的答案,我在我的代碼中使用,它工作正常。 –

相關問題