且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从一个活动转移的图像O另一个节目强制关闭错误

更新时间:2023-01-07 19:38:22

试图去code你的形象,请检查您已经通过文件名是一个有效的之前。唯一的例外似乎是抱怨的文件名是有点无效的,因此作为NPE文件不包含手柄一个有效文件。
所以基本上把新文件()......的号召外脱code,检查该文件是一个有效的对象,如果是去code吧。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img = (ImageView)findViewById(R.id.ImageView01);

    ((Button) findViewById(R.id.Button01))
            .setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
                }
            });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path :" +
                    " " + selectedImagePath);
            img.setImageURI(selectedImageUri);
            Intent intent = new Intent(MainActivity.this, upload.class);
            intent.putExtra("selected image",selectedImageUri);  
            startActivity(intent);

        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);

}

and this is my 2nd activity

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activityfcy_main);
     ImageView imgv = (ImageView) findViewById(R.id.ImageView01);
     Bitmap bitmap = decodeFile(new File(getIntent().getExtras().getString("selected image")));
     imgv.setImageBitmap(bitmap); 
}

Ok now when i load image from gallery it show force close error

Here's the crash:

06-15 17:18:33.744: E/AndroidRuntime(402): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-15 17:18:33.744: E/AndroidRuntime(402): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-15 17:18:33.744: E/AndroidRuntime(402): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-15 17:18:33.744: E/AndroidRuntime(402): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 17:18:33.744: E/AndroidRuntime(402): at android.os.Looper.loop(Looper.java:123)
06-15 17:18:33.744: E/AndroidRuntime(402): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-15 17:18:33.744: E/AndroidRuntime(402): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 17:18:33.744: E/AndroidRuntime(402): at java.lang.reflect.Method.invoke(Method.java:521)
06-15 17:18:33.744: E/AndroidRuntime(402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-15 17:18:33.744: E/AndroidRuntime(402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-15 17:18:33.744: E/AndroidRuntime(402): at dalvik.system.NativeStart.main(Native Method) 
06-15 17:18:33.744: E/AndroidRuntime(402): Caused by: java.lang.NullPointerException
06-15 17:18:33.744: E/AndroidRuntime(402): at java.io.File.fixSlashes(File.java:234)
06-15 17:18:33.744: E/AndroidRuntime(402): at java.io.File.init(File.java:201)
06-15 17:18:33.744: E/AndroidRuntime(402): at java.io.File.(File.java:152)
06-15 17:18:33.744: E/AndroidRuntime(402): at com.example.newprojimage.upload.onCreate(upload.java:18)
06-15 17:18:33.744: E/AndroidRuntime(402): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 17:18:33.744: E/AndroidRuntime(402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-15 17:18:33.744: E/AndroidRuntime(402): ... 11 more

Before trying to decode your image, try checking that the file name you've passed is a valid one. The exception seems like complaining that the filename is somewhat invalid and therefore the NPE as File doesn't contain a handle to a valid file. So basically put the "new File()...." outside the call to decode, check that File is a valid object, and if yes decode it.