用相机拍照后无法恢复活动

时间:2016-11-18 10:26:25

标签: android android-activity camera android-savedstate

我试图制作一个活动,我可以使用相机或图库将图像上传到ima​​geView。
在我的平板电脑上工作正常,但在我的星系s4上,用相机拍照后我无法恢复活动。这是logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.gianluca.achieveme, PID: 27301
              java.lang.RuntimeException: Unable to resume activity {com.example.gianluca.achieveme/com.example.gianluca.achieveme.AchievementCompleted}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.gianluca.achieveme/com.example.gianluca.achieveme.AchievementCompleted}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference
                  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3403)
                  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3434)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4471)
                  at android.app.ActivityThread.access$1000(ActivityThread.java:177)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1455)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:5951)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
               Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.gianluca.achieveme/com.example.gianluca.achieveme.AchievementCompleted}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4067)
                  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3389)
                  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3434) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772) 
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4471) 
                  at android.app.ActivityThread.access$1000(ActivityThread.java:177) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1455) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:145) 
                  at android.app.ActivityThread.main(ActivityThread.java:5951) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference
                  at com.example.gianluca.achieveme.AchievementCompleted.onActivityResult(AchievementCompleted.java:126)
                  at android.app.Activity.dispatchActivityResult(Activity.java:6549)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4063)
                  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3389) 
                  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3434) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772) 
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4471) 
                  at android.app.ActivityThread.access$1000(ActivityThread.java:177) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1455) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:145) 
                  at android.app.ActivityThread.main(ActivityThread.java:5951) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

我读到它是因为使用相机会浪费大量内存,因此我使用onSavedInstanceState和onRestoreInstanceState但我无法知道如何操作。你能解释一下吗?这里也是我活动的java代码:

public class AchievementCompleted extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{

public static TextView tvTitle;
public static TextView tvDescription;
public static Button bDate;
public static Button bPosition;
public static Switch switchbutton;

private Uri imageCaptureUri;
private ImageView mImageView;
private static final int PICK_FROM_CAMERA=1;
private static final int PICK_FROM_FILE=2;

private Button bPhoto;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_achievement_completed);

    tvTitle = (TextView)findViewById(R.id.titolo);
    tvDescription = (TextView)findViewById(R.id.descrizione);
    bDate = (Button)findViewById(R.id.data);
    switchbutton = (Switch)findViewById(R.id.swtich);

    Intent intent = getIntent();
    tvTitle.setText(intent.getStringExtra("Titolo"));
    tvDescription.setText(intent.getStringExtra("Descrizione"));

    switchbutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        }
    });

    final String[] items = new String [] {"Scatta foto", "Scegli dalla galleria"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,items);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Seleziona l'immagine");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(which==0){
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = new File(Environment.getExternalStorageDirectory(),"tmp_avatar"+ String.valueOf(System.currentTimeMillis())+".jpg");
                imageCaptureUri = Uri.fromFile(file);
                try{
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,imageCaptureUri);
                    intent.putExtra("return data",true);

                    startActivityForResult(intent,PICK_FROM_CAMERA);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
                dialog.cancel();
            }else{
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Completa l'azione usando"),PICK_FROM_FILE);
            }
        }
    });
    final AlertDialog dialog=builder.create();
    mImageView=(ImageView)findViewById(R.id.immagine);
    bPhoto = (Button)findViewById(R.id.btnFoto);
    bPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.show();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode!=RESULT_OK)
        return;
    Bitmap bitmap = null;
    String path="";
    if(requestCode==PICK_FROM_FILE){
        imageCaptureUri=data.getData();
        path=getRealPathFromURI(imageCaptureUri);
        if(path==null)
            path=imageCaptureUri.getPath();
        if(path!=null){
            BitmapFactory.Options mBitMapOption = new BitmapFactory.Options();
            mBitMapOption.inSampleSize=2;
            bitmap= BitmapFactory.decodeFile(path,mBitMapOption);
        }

    }else{
        path=imageCaptureUri.getPath();
        BitmapFactory.Options mBitMapOption = new BitmapFactory.Options();
        mBitMapOption.inSampleSize=2;
        bitmap= BitmapFactory.decodeFile(path,mBitMapOption);
    }
    mImageView.setImageBitmap(bitmap);
}

public String getRealPathFromURI(Uri contentURI){
    String[] proj={MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(contentURI,proj,null,null,null);
    if(cursor==null) return null;
    int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
}

提前谢谢

2 个答案:

答案 0 :(得分:0)

imageCaptureUri永远不会被初始化。切换这两行:

if(requestCode==PICK_FROM_FILE) { imageCaptureUri=data.getData();

imageCaptureUri=data.getData();
if(requestCode==PICK_FROM_FILE) {    

所以初始化发生了。

答案 1 :(得分:0)

我也遇到了一些三星标签的同样问题 请在清单中添加 android:configChanges="orientation|screenSize"