从手机存储上传图像

时间:2015-04-25 18:29:38

标签: android libgdx

我正在努力让用户将图片文件从他/她的手机库上传到我的应用中。我设法调用了库,但是当用户选择图像时。我收到以下错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/7963 (has extras) }} to activity {com.progrmor.tombstone.android/com.progrmor.tombstone.android.AndroidLauncher}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/7963 from pid=16802, uid=10409 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4060)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4103)
        at android.app.ActivityThread.access$1400(ActivityThread.java:177)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1497)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5944)
        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:1389)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
 Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/7963 from pid=16802, uid=10409 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

以下是调用图库的代码:

public void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);
    if(response == RESULT_OK) {
        if(request == SELECT_PICTURE) {
            Uri selectedImage = data.getData();
            selectedImagePath = getPath(selectedImage);
        }
    }
}

public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    // this is our fallback here
    return uri.getPath();
}

@Override
public void imageUpload() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent();
            intent.setType("image/");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
        }
    });

}

然后我只在我的一个屏幕类中调用该方法:

boxImage1.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            imageUpload();
        }
    });
}

所以我要问的是:如何点击图片将实际图片上传到纹理或其他内容?我只是希望他们选择的图像在他们选择时在屏幕上绘制。

1 个答案:

答案 0 :(得分:2)

需要在清单中添加适当的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />