如何从SD卡库中选择图像而不是其他来源

时间:2013-08-30 12:57:28

标签: android android-intent gallery

我正在创建一个应用,让用户从手机图库中选择一张图片。一切都适用于SD卡上的图像,但当用户从Picassa中选择一张图片时,应用程序崩溃。

调用图库的代码:

Intent i = new Intent(Intent.ACTION_PICK,
   android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_FROM_GALLERY_ACTION);              

接收图片的代码:

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 filePath = cursor.getString(columnIndex);
cursor.close();
File galeryFile = new File(filePath);
FileUtils.copyFile(galeryFile, imageFile);
processImage(imageFile);

Errror:

08-30 14:51:04.268: E/ActivityThread(31379): Failed to find provider info for com.android.gallery3d.provider
08-30 14:51:04.278: E/AndroidRuntime(31379): FATAL EXCEPTION: main
08-30 14:51:04.278: E/AndroidRuntime(31379): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4353, result=-1, data=Intent { dat=content://com.android.gallery3d.provider/picasa/item/5735585761449205042 flg=0x1 }} to activity {com.example.my_gallery_app/com.example.my_gallery_app.MainActivity}: java.lang.NullPointerException

空行指针在线发生:

cursor.moveToFirst();

I read this workaround.但是评论并不乐观。我想限制画廊向用户展示Facebook,Picassa或其他图片,让她只从SD卡中选择图像。

1 个答案:

答案 0 :(得分:1)

希望这有助于--->

宣言--->

    private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
    Cursor imagecursor;

在onCreate()----->

            String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    String orderBy = MediaStore.Images.Media._ID;
    imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);


    this.count = imagecursor.getCount();
    this.thumbnails = new Bitmap[this.count];
    this.arrPath = new String[this.count];
    this.thumbnailsselection = new boolean[this.count];

    for (int i = 0; i < this.count; i++) {
        imagecursor.moveToPosition(i);
        int id = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);

        thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                getApplicationContext().getContentResolver(), id,
                MediaStore.Images.Thumbnails.MICRO_KIND, null);

        arrPath[i]= imagecursor.getString(dataColumnIndex);
    }

在布局中创建GridView并在此处使用---&gt;

    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);