无法从相机/保管箱加载图片

时间:2013-10-13 19:08:53

标签: java android

使用此功能我只能从图库中加载图片,但我无法从文件系统或Dropbox上传图片,或从相机获取图片:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) return;

    switch (requestCode) {
        case PICK_FROM_CAMERA:

            this.picturePath = picturePath;

            Intent intent1 = new Intent(getBaseContext(), AndroidPaint.class);
            startActivity(intent1);
            break;
        case PICK_FROM_FILE:
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
           Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            this.picturePath = picturePath;
            cursor.close();
            Intent intent = new Intent(getBaseContext(), AndroidPaint.class);
            startActivity(intent);

            //  doCrop();

            break;


    }
}

如果有人有任何建议请帮助我,我想从所有设置上传图片。

问题出在这里:

    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=file:///mnt/sdcard/Android/data/com.dropbox.android/files/scratch/appSetpaint4fun.png typ=image/png }} to activity {com.slikar.slika/slikar.Main.FirstPage}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3103)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3146)
    at android.app.ActivityThread.access$1100(ActivityThread.java:132)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1187)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4575)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at slikar.Main.FirstPage.onActivityResult(FirstPage.java:396)
    at android.app.Activity.dispatchActivityResult(Activity.java:4649)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3099)
    ... 11 more

错误出现在这部分代码中:

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

1 个答案:

答案 0 :(得分:0)

尝试我在网上找到的这门课程

public class BrowsePicture extends Activity {

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((Button) findViewById(R.id.Button01))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {

                    // in onCreate or any event where your want the user to
                    // select a file
                    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);
        }
    }
}

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);
}

}