从Camera上传文件 - 在android

时间:2016-07-20 16:10:52

标签: android

我有关于从相机发送图像的问题。当我按下图像按钮时,将应用“openImageIntent()”功能。一切都有效,直到调用getPath(uri)。当uri被视为空对象时,这会引发错误。这个代码之前正在运行并且现在它崩溃了,这很奇怪。我附加了openImageIntent函数,onResult崩溃的地方和错误。

我将不胜感激任何帮助。提前谢谢。

PS 从图库中附加文件。

 private void openImageIntent() {
// Determine Uri of camera image to save.
        Log.e("Start","yoyoyo");
        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Eventime" + File.separator);
        root.mkdirs();
        final String fname = UUID.randomUUID().toString() + ".png";
        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
        Log.e("CheckOutput", outputFileUri.toString());

        // 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);
            Log.e("CheckOutput", outputFileUri.toString());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // 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()]));

        startActivityForResult(chooserIntent, PICTURE_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (check) {
            if (resultCode == RESULT_OK) {
                if (requestCode == PICTURE_REQUEST_CODE) {
                    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 uri;
                    if (isCamera) {
                        //selectedImageUri
                        uri = outputFileUri;

                    } else {
                        //uri = data.getData();
                        //selectedImageUri
                        uri = data == null ? null : data.getData();
                    }

                    try {
                        path = getPath(uri);
//                beginUpload(path);
                    } catch (URISyntaxException e) {
                        Toast.makeText(this,
                                "Unable to get the file from the given URI.  See error log for details",
                                Toast.LENGTH_LONG).show();
                        Log.e("Upload Imagine", "Unable to upload file from the given uri", e);
                    }

                }
            }
            if (requestCode == 10) {
                if (resultCode == RESULT_OK) {
                    String name = null;
                    if (data.hasExtra("name")) {
                        name = data.getStringExtra("name");
                    }
                    exit(name);
                }
            }
            check = false;
        }
    }

错误日志

致命例外:主程序:com.amazon.mysampleapp,PID:12899

java.lang.RuntimeException: Unable to resume activity {com.amazon.mysampleapp/com.mysampleapp.RegisterUser}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.amazon.mysampleapp/com.mysampleapp.RegisterUser}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.net.Uri.getPathSegments()' on a null object reference
                                                                            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3392)
                                                                            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3423)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2761)
                                                                            at 
                                                                         Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.amazon.mysampleapp/com.mysampleapp.RegisterUser}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.net.Uri.getPathSegments()' on a null object reference
                                                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.net.Uri.getPathSegments()' on a null object reference
                                                                            at android.provider.DocumentsContract.isDocumentUri(DocumentsContract.java:698)
                                                                            at com.mysampleapp.RegisterUser.getPath(RegisterUser.java:346)
                                                                            at com.mysampleapp.RegisterUser.onActivityResult(RegisterUser.java:199)

2 个答案:

答案 0 :(得分:0)

我假设outputFileUri是您的活动/片段中此代码所在的字段。如果是这样,您需要确保在onSaveInstanceState() Bundle中加入此内容并将其恢复为onCreate()onRestoreInstanceState()。当摄像头应用程序位于前台时,您的进程可以终止,因此Uri需要成为正在恢复的状态的一部分。

答案 1 :(得分:0)

或者您可以在outputFileUri内分配onActivityResult的值,因为openImageIntent()中不需要分配。

<强>通过:

final String fname = UUID.randomUUID().toString() + ".png";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);

<强>之前:

uri = outputFileUri;

希望得到这个帮助。