2015-06-21 67 views
0

我想將攝像機返回的圖像設置爲CropImageView。 使用的庫是this無法將圖像設置爲攝像機返回的ImageView

正確設置圖庫返回的圖像。

但是對於相機返回的圖像,當設置爲setImageUri()時,ImageView是空的,儘管它在日誌中顯示圖像的Uri

這是通過登錄onActivityResult

文件顯示:///storage/emulated/0/Android/data/com.android.example/cache/pickImageResult.jpeg

當試圖與設置setImageBitmap(),Bitmap photo = (Bitmap) data.getExtras().get("data")結果爲NullPointerException

的代碼是

public class ImagePickerActivity extends AppCompatActivity { 

private CropImageView mCropImageView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_image_picker); 
    Log.d("image picker", "activity created"); 
    mCropImageView = (CropImageView) findViewById(R.id.cropImageView); 

} 

/** 
* On load image button click, start pick image chooser activity. 
*/ 
public void onLoadImageClick(View view) { 
    startActivityForResult(getPickImageChooserIntent(), 200); 
} 

/** 
* Crop the image and set it back to the cropping view. 
*/ 
public void onCropImageClick(View view) { 
    Bitmap cropped = mCropImageView.getCroppedImage(500, 500); 
    if (cropped != null) 
     mCropImageView.setImageBitmap(cropped); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     Uri imageUri = getPickImageResultUri(data); 
     Log.d("image picker", imageUri.toString()); 

     /*Bitmap photo = (Bitmap) data.getExtras().get("data"); 
     mCropImageView.setImageBitmap(photo);*/ 

     mCropImageView.setImageUri(imageUri); 
    } 
} 

/** 
* Create a chooser intent to select the source to get image from.<br/> 
* The source can be camera's (ACTION_IMAGE_CAPTURE) or gallery's (ACTION_GET_CONTENT).<br/> 
* All possible sources are added to the intent chooser. 
*/ 
public Intent getPickImageChooserIntent() { 

    // Determine Uri of camera image to save. 
    Uri outputFileUri = getCaptureImageOutputUri(); 

    List<Intent> allIntents = new ArrayList<>(); 
    PackageManager packageManager = getPackageManager(); 

    // collect all camera intents 
    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); 
    for (ResolveInfo res : listCam) { 
     Intent intent = new Intent(captureIntent); 
     intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
     intent.setPackage(res.activityInfo.packageName); 
     if (outputFileUri != null) { 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     } 
     allIntents.add(intent); 
    } 

    // collect all gallery intents 
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT); 
    galleryIntent.setType("image/*"); 
    List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0); 
    for (ResolveInfo res : listGallery) { 
     Intent intent = new Intent(galleryIntent); 
     intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
     intent.setPackage(res.activityInfo.packageName); 
     allIntents.add(intent); 
    } 

    // the main intent is the last in the list so pickup the useless one 
    Intent mainIntent = allIntents.get(allIntents.size() - 1); 
    for (Intent intent : allIntents) { 
     if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) { 
      mainIntent = intent; 
      break; 
     } 
    } 
    allIntents.remove(mainIntent); 

    // Create a chooser from the main intent 
    Intent chooserIntent = Intent.createChooser(mainIntent, "Select source"); 

    // Add all other intents 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()])); 

    return chooserIntent; 
} 

/** 
* Get URI to image received from capture by camera. 
*/ 
private Uri getCaptureImageOutputUri() { 
    Uri outputFileUri = null; 
    File getImage = getExternalCacheDir(); 
    if (getImage != null) { 

     outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg")); 
    } 
    return outputFileUri; 
} 

/** 
* Get the URI of the selected image from {@link #getPickImageChooserIntent()}.<br/> 
* Will return the correct URI for camera and gallery image. 
* 
* @param data the returned data of the activity result 
*/ 
public Uri getPickImageResultUri(Intent data) { 
    boolean isCamera = true; 
    if (data != null) { 
     String action = data.getAction(); 
     isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE); 
    } 
    return isCamera ? getCaptureImageOutputUri() : data.getData(); 
} 

}

請幫助解決這個問題。提前致謝。 該代碼取自here

+0

您是否正在'onActivityResult'獲取數據? – Pankaj

+0

是的。就像我寫的,數據存在。奇怪的是,登錄'onActivityResult'顯示'Uri'。但是'Bitmap photo =(Bitmap)data.getExtras()。get(「data」)'返回'NullPointerException'。 –

+0

只是做'data.getData();'而不是'data.getExtras()。get(「data」)' – Pankaj

回答

-1

ü可以將這段代碼

Uri imageUri = getPickImageResultUri(data); 
Log.d("image picker", imageUri.toString()); 
mCropImageView.setImageUri(imageUri); 

與..這,

Uri imageUri = data.getData(); 
path = GetPathFromURI(context, imageUri); 
bitmap = BitmapFactory.decodeFile(path); 
mCropImageView.setImageBitmap(bitmap); 

GetPathFromURI

public static String GetPathFromURI(Context context,Uri contentUri) { 
     String realPath = ""; 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     ContentResolver cr = context.getContentResolver(); 
     Cursor cursor = cr.query(contentUri, projection, null, null, null); 
     if (cursor.moveToFirst()) { 
      realPath = cursor.getString(0); 
     } 
     cursor.close(); 
     return realPath; 
    } 
+0

問題不在於裁剪。問題是圖像沒有從相機加載。 裁剪正常發生。 –

+0

@PratikBorole可以ü請檢查這個 –

+0

因此,'onActivityResult'的'Intent data'爲null。 因爲我得到'嘗試調用虛擬方法'android。net.Uri android.content.Intent.getData()'日誌中的空對象引用'消息。我該怎麼辦? –

0

對於我使用下面的代碼從攝像機獲取圖像,其在棒棒糖工作也:

private void captureFromCamera() 
       Calendar calendar = Calendar.getInstance(); 
       String fileName = String.valueOf(calendar.getTimeInMillis()); 
       fileName += ".jpg"; 

       ContentValues values = new ContentValues(); 

       values.put(MediaStore.Images.Media.TITLE, fileName); 

       values.put(MediaStore.Images.Media.DESCRIPTION, 
         "Image capture by camera"); 

       Uri imageUri = activity.getContentResolver().insert(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

       new DevicePreferences().addKey(activity, Constants.IMAGE_URI, 
         imageUri.toString()); // Storing in shared preference and in onActivityResult getting uri from shared preference 

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

       intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

       intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

       startActivityForResult(intent, 
         CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
    } 
+1

它的工作原理,但不是我的Galaxy S4運行5.0.1。嘗試了三個其他手機。完美地運行在他們身上。 –

+0

你在手機上發生了什麼錯誤? – Pankaj

+0

沒有錯誤。只是一個空白的'ImageView'。正如我之前提到的,日誌顯示了一個有效的'Uri'。 –

相關問題