4
 public void uploadpicture(View view)
        {
            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);
                    Toast.makeText(this,selectedImagePath,Toast.LENGTH_SHORT).show();
                    Log.i("selectedImagePath",selectedImagePath.trim());

                }

            }


        }

我从 Log-cat 那里得到了这个:

05-24 15:57:51.054: E/AndroidRuntime(18016): FATAL EXCEPTION: main
05-24 15:57:51.054: E/AndroidRuntime(18016): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.sec.gallery3d.provider/picasa/item/5147009501910019474 }} to activity {ncpl.talentapp/ncpl.talentapp.SignUp}: java.lang.NullPointerException
05-24 15:57:51.054: E/AndroidRuntime(18016):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2821)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2864)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at android.app.ActivityThread.access$1000(ActivityThread.java:122)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1057)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at android.os.Looper.loop(Looper.java:132)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at android.app.ActivityThread.main(ActivityThread.java:4126)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at java.lang.reflect.Method.invoke(Method.java:491)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
05-24 15:57:51.054: E/AndroidRuntime(18016):    at dalvik.system.NativeStart.main(Native Method)
05-24 15:57:51.054: E/AndroidRuntime(18016): Caused by: java.lang.NullPointerException
4

4 回答 4

9

从您的日志看来,您正在尝试从 Picasa 文件夹中选择图像dat=content://com.android.sec.gallery3d.provider/picasa/item/5147009501910019474,并且必须以不同方式处理这些图像,因为MediaStore.Images.Media.DATA列中的数据对于它们来说将为空。您可以在此链接中了解如何正确处理从 Picasa 中挑选图片:

http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/

只有您还需要处理以“content://com.sec.android.gallery3d”开头的 URI(我在三星 S3 上看到)所以改变

if (selectedImage.toString().startsWith("content://com.google.android.gallery3d"))

if (_selectedImage.toString().startsWith("content://com.google.android.gallery3d") 
    || selectedImage.toString().startsWith("content://com.sec.android.gallery3d"))

我不确定是否还有其他特殊情况(可能是)。

于 2012-09-19T08:56:43.317 回答
5

我遇到了同样的问题,并尝试根据推荐的网站 user1682516 来实现它。因为我的 Picasa 内容 URL 不完全匹配,所以我把头撞到了墙上:我的看起来像“com.sec.android.gallery3d.provider”而不是“com.android.gallery3d.provider”,但似乎不匹配无论我是否更改了 URL,都可以工作。

然后,当我最终意识到我用来在我的应用程序中为 ImageViews 加载远程图像的库https://github.com/nostra13/Android-Universal-Image-Loader为我解决了这个问题时,我感到很愚蠢。我可以使用适当的参数调用 ImageLoader.getInstance().loadImage() 并在回调中获取位图。我不需要担心它是本地图像、远程 Picasa 图像还是普通的远程图像——这由我负责。希望我几个小时前就知道了!

于 2013-06-19T19:05:26.767 回答
0

在三星 SM-A520F 上测试。如果您只想从图库中选择本地图像,则可以使用:

fun uploadpicture(view: View?) {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true) // Restrict choice
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE)
    }

于 2021-10-11T07:44:41.933 回答
-2

测试代码在这里

public class SelectPhotoActivity extends Activity {

private static final int SELECT_PICTURE = 1;
private String selectedImagePath="";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivityForResult(intent, SELECT_PICTURE); 
}

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

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE)
        {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
           // here you can set the image
            }
    }
}

}
于 2012-05-24T10:33:59.653 回答