相机图片返回null url。 (机器人)

时间:2014-03-11 23:40:38

标签: android android-camera

每次我用相机拍照时,它都会将selectedImageUri设置为null ...

此外,DATA IS非NULL并且ACTION IS NOT NULL被调用,因为我的困惑......我不确定我是否做错了让我知道,拍摄时的selectedImageUri应该返回为Uri和不为空

这是我的代码:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
                final boolean isCamera;
                if (data == null) {
                    Log.e("data", "DATA IS NULL");
                    isCamera = true;
                } else {
                    Log.e("data", "DATA IS NOT NULL");
                    final String action = data.getAction();
                    //Log.e("PICTURE PATH?", action);
                    if (action == null) {
                        Log.e("data", "ACTION IS NULL");
                        isCamera = false;
                    } else {
                        Log.e("data", "ACTION IS NOT NULL");
                        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                    }
                }

                Uri selectedImageUri;
                if (isCamera) {
                    selectedImageUri = outputFileUri;
                    String uploadurl = "discover/p";
                    File photoSelected = new File(selectedImageUri.getPath());
                    TypedFile photo = new TypedFile("application/octet-stream", photoSelected);
                    String userId = "userId1";
                    Callback cameraResponse = new Callback() {
                        @Override
                        public void success(Object o, Response response) {

                        }

                        @Override
                        public void failure(RetrofitError retrofitError) {
                            Log.e("TAG", retrofitError.getMessage());
                        }
                    };
                    uploadImage(uploadurl, photo, userId, cameraResponse);
                    Picasso.with(getActivity()).load(selectedImageUri.getPath()).into(profilePicture);
                } else {
                    if (data == null) {
                        selectedImageUri = null;
                    } else {
                        selectedImageUri = data.getData();
                        Log.e("gallery image", "link is " + data.getData());
                        String uploadurl = "discover/p";
                        File photoSelected = new File(String.valueOf(selectedImageUri));
                        TypedFile photo = new TypedFile("application/octet-stream", photoSelected);
                        String userId = "userId1";
                        Callback galleryResponse = new Callback() {
                            @Override
                            public void success(Object o, Response response) {

                            }

                            @Override
                            public void failure(RetrofitError retrofitError) {

                            }
                        };
                        uploadImage(uploadurl, photo, userId, galleryResponse);

                        Picasso.with(getActivity()).load(selectedImageUri).into(profilePicture);
                    }

                }
            }
        }
    }

编辑包括相机代码:

if(isDeviceSupportCamera()) {
                // Determine Uri of camera image to save.
                final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
                root.mkdirs();
                final String fname = getUniqueImageFilename();
                final File sdImageMainDirectory = new File(root, fname);
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                Log.e("is camera ", "even getting CALLED CAM1111?");
                // Camera.
                final List<Intent> cameraIntents = new ArrayList<Intent>();
                Log.e("is camera ", "even getting CALLED CAM?222");
                final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                final PackageManager packageManager = getActivity().getPackageManager();
                final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
                Log.e("is camera ", "even getting CALLED CAM333?");
                for (ResolveInfo res : listCam) {
                    Log.e("is camera ", "even getting CALLED CAM4444?");
                    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, outputFileUri);
                    cameraIntents.add(intent);
                }
                Log.e("is camera ", "even getting CALLED CAM?5555");

                final Intent chooserCameraIntent = Intent.createChooser(captureIntent, "Use Camera");

                startActivityForResult(chooserCameraIntent, YOUR_SELECT_PICTURE_REQUEST_CODE);

                } else {
                     Toast.makeText(getActivity(), "There is no camera available on this device.", Toast.LENGTH_SHORT).show();
                }

            }

更新

日志文件:

03-12 10:20:21.854  27814-27814/com.example.app E/data﹕ CAMERA IS NOT NULL outputfileuri is file:///storage/emulated/0/MyDir/img_userID1%20_1394612413661.jpg
03-12 10:20:21.854  27814-27814/com.example.app E/data﹕ camera link for selectedImageUri file:///storage/emulated/0/MyDir/img_userID1%20_1394612413661.jpg
03-12 10:20:21.854  27814-27814/com.example.app E/data﹕ selected image get path link is /storage/emulated/0/MyDir/img_userID1 _1394612413661.jpg

我还将isCamera添加为true而不是

action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

如果我在if request code

下添加此代码
 if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
//                Bundle extras = data.getExtras();
//                Bitmap imageBitmap = (Bitmap) extras.get("data");
//                profilePicture.setImageBitmap(imageBitmap);
               final boolean isCamera;

它实际上是在图像视图中显示图像...所以我很困惑为什么它没有让uri正确。

这是android清单权限

<!-- For Camera use and downloading image -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- internet -->
    <uses-permission android:name="android.permission.INTERNET" />

1 个答案:

答案 0 :(得分:1)

您的活动可能已被系统销毁,以释放相机应用程序的内存以处理您的意图。您可以使用onSaveInstanceState()保存状态并将其恢复为onCreate()。参见例如Android startCamera gives me null Intent and ... does it destroy my global variable?