【问题标题】:How to pick images from SD card gallery and not other sources如何从 SD 卡库而不是其他来源中选择图像
【发布时间】:2013-08-30 12:57:28
【问题描述】:

我正在创建一个允许用户从手机图库中挑选图片的应用。对于 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);

错误:

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. 但是 cmets 并不乐观。我想限制画廊向用户显示 Facebook、Picassa 或其他图片,并让她只从 SD 卡中挑选图片。

【问题讨论】:

    标签: android android-intent gallery


    【解决方案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);
        }
    

    在Layout中创建GridView并在这里使用--->

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

    【讨论】:

    • 我希望找到一个使用内置图库来显示图像的解决方案,但我会尝试一下,然后回复您!
    猜你喜欢
    • 2012-07-26
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多