首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Uri.fromFile是否返回null?

Uri.fromFile是否返回null?
EN

Stack Overflow用户
提问于 2016-08-10 08:13:10
回答 1查看 638关注 0票数 0

由于某种原因,我的onActivityResult中的outputFileUri返回null:

代码语言:javascript
复制
Uri selectedImageUri;
if (isCamera) {
    selectedImageUri = outputFileUri;

我不明白为什么。请看一下我的代码,看看我哪里出了问题。我允许用户通过从图库中选择或使用相机拍摄4张图像。用户用相机拍摄的照片不会显示,可能是因为它们的uri为空。outputFileUri是一个类变量。

takePictureIntent()

代码语言:javascript
复制
private void dispatchTakePictureIntent() {
    for(int i = 0; i < 4; i++) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
                outputFileUri = Uri.fromFile(photoFile);
            } catch (IOException ex) {
                Log.w("error","IOException");
            }catch (NullPointerException nullEx) {
                Log.w("error","NullPointerException");
            }
            // Camera.
            final List<Intent> cameraIntents = new ArrayList<Intent>();
            final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            final PackageManager packageManager = getPackageManager();
            final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
            for (ResolveInfo res : listCam) {
                final String packageName = res.activityInfo.packageName;
                final Intent intent = new Intent(captureIntent);
                intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                intent.setPackage(packageName);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                cameraIntents.add(intent);
            }
            // Filesystem.
            final Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
            // Chooser of filesystem options.
            final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
            // Add the camera options.
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
            if(id.equals(HAPPY_ID))
                startActivityForResult(chooserIntent, REQUEST_HAPPY_PHOTO);
            if(id.equals(SURPRISED_ID))
                startActivityForResult(chooserIntent, REQUEST_SURPRISED_PHOTO);
            if(id.equals(AFRAID_ID))
                startActivityForResult(chooserIntent, REQUEST_AFRAID_PHOTO);
            if(id.equals(UPSET_ID))
                startActivityForResult(chooserIntent, REQUEST_UPSET_PHOTO);
            if(id.equals(SAD_ID))
                startActivityForResult(chooserIntent, REQUEST_SAD_PHOTO);
        }
    }
}

onActivityResult()

代码语言:javascript
复制
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_HAPPY_PHOTO || requestCode == REQUEST_SURPRISED_PHOTO || requestCode == REQUEST_AFRAID_PHOTO ||
                    requestCode == REQUEST_UPSET_PHOTO || requestCode == REQUEST_SAD_PHOTO) {
                final boolean isCamera;
                if (data == null) {
                    isCamera = true;
                } else {
                    final String action = data.getAction();
                    if (action == null) {
                        isCamera = false;
                    } else {
                        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    }
                }
                Uri selectedImageUri;
                if (isCamera) {
                    selectedImageUri = outputFileUri;
                } else {
                    selectedImageUri = data == null ? null : data.getData();
                }
                //Log.d("doing ids", "right before id");
                //Log.d("doing ids", "id is " + id);
                if(requestCode == REQUEST_HAPPY_PHOTO) {
                    //Log.d("doing ids", "in happy");
                    happyList.add(selectedImageUri);
                }
                if(requestCode == REQUEST_SURPRISED_PHOTO) {
                    //Log.d("doing ids", "in surprised");
                    surprisedList.add(selectedImageUri);
                }
                if(requestCode == REQUEST_AFRAID_PHOTO) {
                    //Log.d("doing ids", "in surprised");
                    afraidList.add(selectedImageUri);
                }
                if(requestCode == REQUEST_UPSET_PHOTO) {
                    //Log.d("doing ids", "in surprised");
                    upsetList.add(selectedImageUri);
                }
                if(requestCode == REQUEST_SAD_PHOTO) {
                    //Log.d("doing ids", "in surprised");
                    sadList.add(selectedImageUri);
                }
            }
        }
    }

My createImageFile():

代码语言:javascript
复制
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp;
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

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

回答 1

Stack Overflow用户

发布于 2016-08-10 08:47:55

我遇到了类似的问题,我通过持有对传递给Intent的URI的引用来解决它。这允许我不依赖于返回URI的意图。

代码如下:

代码语言:javascript
复制
private Uri startCameraFileUri;

private void startCameraIntent() {
    final Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        startCameraFileUri = Uri.fromFile(Utils.getNewFile());
        intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, startCameraFileUri);
        intentCamera.putExtra("return-data", true);
        startActivityForResult(intentCamera, IConstants.REQUEST_TAKE_PICTURE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void openGalleryIntent() {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    if (isKitKat) {
        final Intent openGalleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        openGalleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
        openGalleryIntent.setType("image/*");
        openGalleryIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
        startActivityForResult(
                Intent.createChooser(openGalleryIntent, getString(R.string.PROFILE_PIC_LIBRARY)),
                IConstants.REQUEST_GALLERY);
    } else {
        final Intent openGalleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        openGalleryIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
        startActivityForResult(Intent.createChooser(openGalleryIntent, getString(R.string.PROFILE_PIC_LIBRARY)), IConstants.REQUEST_GALLERY);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {

        final Uri uri;
        if(data != null) {
            uri = data.getData();
        }
        else{
            uri = startCameraFileUri;
        }
        switch (requestCode) {
            case IConstants.REQUEST_GALLERY:
            case IConstants.REQUEST_TAKE_PICTURE:
                try {
                    if (!uri.toString().isEmpty()) {
                        loadUserPhoto(uri.toString());

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38862340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档