在4.4 Camera活动返回null

时间:2017-09-06 18:38:28

标签: android

当我在android 4.2上运行这个应用程序时,位图会返回图像,但是在4.4中它会返回null ........在图库中,一切都很完美。

  if (items[which].equals("Camera")) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);

在onActivityResult

 else if (requestCode == 1) {
            final Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
           if (imageNumber == 1)
               slip_image1.setImageBitmap(thumbnail);
            else if (imageNumber == 2)
                slip_image2.setImageBitmap(thumbnail);
            else if (imageNumber == 3)
                slip_image3.setImageBitmap(thumbnail);

1 个答案:

答案 0 :(得分:1)

   @SuppressLint("NewApi")
   public static String getRealPathFromURI_API19(Context context, Uri uri){
    String filePath = "";
    try { // FIXME NPE error when select image from QuickPic, Dropbox etc
        String wholeID = DocumentsContract.getDocumentId(uri);
        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];
        String[] column = {MediaStore.Images.Media.DATA};
        // where id is equal to
        String sel = MediaStore.Images.Media._ID + "=?";
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.INTERNAL_CONTENT_URI,
                column, sel, new String[]{id}, null);
        int columnIndex = cursor.getColumnIndex(column[0]);
        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }

        cursor.close();


        return filePath;
    } catch (Exception e) { // this is the fix lol
        String result;
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = uri.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }
}


@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;

    CursorLoader cursorLoader = new CursorLoader(
            context,
            contentUri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();

    if(cursor != null){
        int column_index =
                cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);
    }

    return result;
}

public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    int column_index
            = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}