我将ACTION_IMAGE_CAPTURE与文档中建议的预定目标uri一起使用。但是,当我尝试在活动获取图像后立即对其进行解码时,decodeStream()失败。如果我几秒钟后再试一次,就可以了。我想文件是在后台异步写入的。我怎样才能知道它什么时候可以使用?
以下是我的代码的关键部分:
确定目标文件名:

String filename = String.format("pic%d.jpg", new Date().getTime());
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);

try {
    file.createNewFile();
} catch (IOException e) {
    file = new File(context.getFilesDir(), filename);
}
targetUri = Uri.fromFile(photoFile);

拍照:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, targetUri);
fragment.startActivityForResult(takePictureIntent, RESULT_TAKE_PICTURE);

onActivityResult()中:
if (resultCode == Activity.RESULT_OK) {
    if (data != null) {
        // Note that data.getData() is null here.
        InputStream is = getContentResolver().openInputStream(targetUri);
        if (is != null) {
            Bitmap bm = BitmapFactory.decodeStream(is);

decodeStream返回空值。如果几秒钟后我再打同样的电话,它就会成功。有什么能告诉我文件什么时候可用吗?
更新:根据greenapps的建议,我先用decodeStream打个inJustDecodeBounds电话,查看尺寸是否是内存问题。原来这个第一个边界只解码传递失败,但现在紧接着的实际解码流调用成功了!如果我再这样做,他们都成功了!
因此,第一次调用decodeStream似乎总是失败,之后的所有调用都很好,即使它们在之后立即发生(=在同一方法中)。所以异步写可能不成问题。但还有别的。但是什么?

最佳答案

if (requestCode == Utility.GALLERY_PICTURE) {

        Uri selectedImageUri = null;
        try {
            selectedImageUri = data.getData();
            if (mImgProfilePic != null) {
                // mImgProfilePic.setImageURI(selectedImageUri);
                mImgProfilePic.setImageBitmap(decodeUri(getActivity(),
                        selectedImageUri, 60));
                // decodeUri

            }
        } catch (Exception e) {

        }
        // //////////////
        try {
            // Bundle extras = data.getExtras();
            // // get the cropped bitmap
            // Bitmap thePic = extras.getParcelable("data");
            // mImgProfilePic.setImageBitmap(thePic);

            final Uri tempUri = selectedImageUri;
            Log.d("check", "uri " + tempUri);
            // http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=companylogo
            upLoadServerUri = "http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=employee_profile_pic&image=";
            upLoadServerUri = Utility.EMPLOYEE_PROFILE_PIC_URL
                    + "&employee_id=" + empId;
            dialog = ProgressDialog.show(getActivity(), "",
                    "Uploading file...", true);

            new Thread(new Runnable() {
                public void run() {
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            // messageText.setText("uploading started.....");
                        }
                    });
                    uploadFilePath = getRealPathFromURI(tempUri);
                    uploadFile(uploadFilePath + "");
                    // uploadFile(tempUri+"");
                }
            }).start();

        } catch (Exception e) {

        }

        // ///////

    }

10-06 06:52