2

我已经搜索了 2 天,试图找到我的问题的答案,但并没有真正找到令人满意的答案。

我正在制作一个应用程序,它调用相机 Intent 并基本上拍摄捕获的照片,将其放入 ImageView 中进行预览,并且我有一个按钮可以将文件保存到我创建的应用程序文件夹中的用户定义文件夹中。

我可以使用 AVD(我的 GF 的 Droid Razr M)很容易地运行我的应用程序。但是当我尝试在我的 GS3 上运行它时,它在图像上点击接受后会死得很惨,除非我在清单,并在我拍照时将手机保持在横向。我认为这与我看到的关于 onActivityResult 数据输入为空的情况有关,因为我像他们一样得到空指针异常。我正在寻找针对我的问题的解决方案。

我的相机意图:

        if (v.getId() == R.id.snap) {
        //start camera up, and set temp file to store image for viewing inside the app
        String identity =  android.os.Build.MANUFACTURER;
        Intent intent1,cameraIntent;
        File f = new File(Environment.getExternalStorageDirectory(), "photo1.jpg");
        try {
        if (identity.equalsIgnoreCase("samsung")) {

            intent1 = new Intent("android.media.action.IMAGE_CAPTURE");
            Toast.makeText(getApplicationContext(), identity, Toast.LENGTH_LONG).show();

            //File f = new File(Environment.getExternalStorageDirectory(), "photo1.jpg");
            mUri=Uri.fromFile(f);
            intent1.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
            startActivityForResult(intent1, TAKE_PICTURE);
            }
        else {
            cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
            Toast.makeText(getApplicationContext(), identity, Toast.LENGTH_LONG).show();

            //File f = new File(Environment.getExternalStorageDirectory(), "photo1.jpg");
            mUri=Uri.fromFile(f);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);

            startActivityForResult(cameraIntent, TAKE_PICTURE);
            }

我知道我真的不必拥有制造商部分,但如果我必须实施三星特定的修复程序,框架就在那里。

我的保存块:

else if (v.getId() == R.id.save){
        //store photo in a bitmap, and set string to make folder to store photos later on
        Bitmap bitmap = mPhoto;
        String root = Environment.getExternalStorageDirectory().toString();
        File newDir;

        if (value.isEmpty())
            newDir = new File(root + "/CamExample/default_folder");

        else
            newDir = new File(root + "/" + "CamExample/" + value);

        //if the chosen directory doesn't exist
        if (!newDir.exists()) {
            newDir.mkdirs();
            Toast.makeText(getApplicationContext(), "folder created successfully", Toast.LENGTH_SHORT).show();
        }

        //if it does exist
        else {
            Toast.makeText(getApplicationContext(), "directory already exists", Toast.LENGTH_SHORT).show();
        }

        //image naming convention to avoid duplicate images, should look like IMG_yyyMMdd_HHmmss.jpg
        String TIMESTAMP = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        String photoname = "IMG_"+TIMESTAMP+".jpg";
        File file1=new File (newDir, photoname);

        if (file1.exists()) {
            file1.delete();
        }

        try {
            //actually create the jpeg file and put it in file1 in newDir
            FileOutputStream out = new FileOutputStream(file1);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
            out.flush();
            out.close();
            ((ImageView)findViewById(R.id.photo_holder)).setImageDrawable(null);
            mPhoto=null;

            Toast.makeText(getApplicationContext(), photoname+" has been saved properly", Toast.LENGTH_SHORT).show();
        }

        catch (Exception e) {
            Toast.makeText(getApplicationContext(), photoname+" has not saved properly", Toast.LENGTH_SHORT).show();
        }
    }

还有我的 onActivityResult 定义:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {

    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            getContentResolver().notifyChange(mUri, null);
            ContentResolver cr = getContentResolver();

            try {
                mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
                ((ImageView)findViewById(R.id.photo_holder)).setImageBitmap(mPhoto);
            }

            catch (Exception e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}

这是我的日志:

08-20 21:58:26.387: E/AndroidRuntime(10541): FATAL EXCEPTION: main
08-20 21:58:26.387: E/AndroidRuntime(10541): java.lang.RuntimeException: Unable to resume activity {com.example.camexample/com.example.camexample.Main}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.camexample/com.example.camexample.Main}: java.lang.NullPointerException
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2639)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2667)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2140)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3576)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.access$800(ActivityThread.java:143)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.os.Looper.loop(Looper.java:137)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.main(ActivityThread.java:4950)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at java.lang.reflect.Method.invokeNative(Native Method)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at java.lang.reflect.Method.invoke(Method.java:511)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at dalvik.system.NativeStart.main(Native Method)
08-20 21:58:26.387: E/AndroidRuntime(10541): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.camexample/com.example.camexample.Main}: java.lang.NullPointerException
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2626)
08-20 21:58:26.387: E/AndroidRuntime(10541):    ... 13 more
08-20 21:58:26.387: E/AndroidRuntime(10541): Caused by: java.lang.NullPointerException
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.os.Parcel.readException(Parcel.java:1431)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.os.Parcel.readException(Parcel.java:1379)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:452)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.content.ContentResolver.notifyChange(ContentResolver.java:1280)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.content.ContentResolver.notifyChange(ContentResolver.java:1259)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at com.example.camexample.Main.onActivityResult(Main.java:190)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.Activity.dispatchActivityResult(Activity.java:5363)
08-20 21:58:26.387: E/AndroidRuntime(10541):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
08-20 21:58:26.387: E/AndroidRuntime(10541):    ... 14 more
4

0 回答 0