没有获取捕获的图像路径

时间:2018-12-10 04:14:16

标签: android android-camera uri

我正在构建一个我上载图像或pdf文件的应用程序。图像可以通过图库上传或捕获图像。捕获图像后上传时,找不到图像的路径。

这就是我打开相机意图的方式

 case 1:
                            File imageFolder = new File(Environment.getExternalStorageDirectory(), "/AFPL");
                            imageFolder.mkdir();
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyymmdd_hhmmss");
                            String timestamp = simpleDateFormat.format(new Date());
                            File image = new File(imageFolder, timestamp+ ".jpg");
                            //    Uri uriImage = Uri.fromFile(image);
                            if (ContextCompat.checkSelfPermission(ClientDocsUpload.this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
                                ActivityCompat.requestPermissions(ClientDocsUpload.this,
                                        new String[]{Manifest.permission.CAMERA,
                                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                                Manifest.permission.READ_EXTERNAL_STORAGE},
                                        ASK_MULTIPLE_REQUEST);
                            }else {
                                camerauri = FileProvider.getUriForFile(ClientDocsUpload.this, BuildConfig.APPLICATION_ID + ".provider", image);
                                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                intent.putExtra(MediaStore.EXTRA_OUTPUT, camerauri);
                                myBundle.putString("type", type);
                                startActivityForResult(intent, 102);
                            }

                            break;

这是onActivityResult

if (requestCode == 102){
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(
                    camerauri,
                    projection, null, null, null);
            int column_index_data = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToLast();

            String path = cursor.getString(column_index_data);


            files = new File(path.substring(0,path.lastIndexOf("/")),path.substring(path.lastIndexOf("/"),path.length()));
            String type = myBundle.getString("type");
            UploadDocs(files,type,"image");
        }

还有一个问题是managedQuery()也被弃用了。

Logcat

W/System.err:     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
    at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:87)
    at tk.smilestech.afpl.ClientDocsUpload.onActivityResult(ClientDocsUpload.java:417)
 W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:6915)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
 W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
    at android.app.ActivityThread.-wrap20(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

2 个答案:

答案 0 :(得分:0)

您可以通过调用以下命令获取图像的取消路径:

recipes_ingredients.

然后,您可以将此路径存储在意图中,并在image.getAbsolutePath(); 中使用它

答案 1 :(得分:0)

考虑到您的评论,这是我写的,谢谢。 @MikeM。

      case 1:
                            File imageFolder = new File(Environment.getExternalStorageDirectory(), "/AFPL");
                            imageFolder.mkdir();
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyymmdd_hhmmss");
                            String timestamp = simpleDateFormat.format(new Date());
                            image = new File(imageFolder, timestamp+ ".jpg");
                            //    Uri uriImage = Uri.fromFile(image);
                            if (ContextCompat.checkSelfPermission(ClientDocsUpload.this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
                                ActivityCompat.requestPermissions(ClientDocsUpload.this,
                                        new String[]{Manifest.permission.CAMERA,
                                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                                Manifest.permission.READ_EXTERNAL_STORAGE},
                                        ASK_MULTIPLE_REQUEST);
                            }else {
                                camerauri = FileProvider.getUriForFile(ClientDocsUpload.this, BuildConfig.APPLICATION_ID + ".provider", image);
                                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                intent.putExtra(MediaStore.EXTRA_OUTPUT, camerauri);
                                myBundle.putString("type", type);
                                startActivityForResult(intent, 102);
                            }

                            break;

onActivityResult

files = image;
String type = myBundle.getString("type");
UploadDocs(files,type,"image");