Android - 图像选择器,错误的图像

时间:2012-04-23 21:11:34

标签: android android-intent action media

我正在开始请求图片选择:

Intent intent = new Intent();
intent.setType( "image/*" );
intent.setAction( Intent.ACTION_GET_CONTENT );
startActivityForResult( Intent.createChooser( intent, "Choose"), PHOTO_GALLERY );

将数据恢复到onActivityResult

if( resultCode == Activity.RESULT_OK && requestCode == PHOTO_GALLERY )
{
  U.log( data.getData() );
  Bitmap bm = ... // built from the getData() Uri
  this.postImagePreview.setImageBitmap( bm );
}

当我启动Intent时,我会看到一些文件夹,例如sdcardDrop BoxMyCameraApp等。

如果我从sdcard中选择了一张图片,当我加载预览时,它就是完全错误的图像。其他文件夹似乎没有给我这个问题。

有谁知道为什么它会让我选择一张图片,然后把Uri给我另一张图片?

编辑:以下是一些示例记录的getData()

好:

content://com.google.android.gallery3d.provider/picasa/item/5668377679792530210

为:

content://media/external/images/media/28

编辑:从图库的sdcard文件夹中挑选时,我仍然遇到问题。

这是我在onActivityResult中所做的更多扩展:

// cursor
Uri selectedImage = data.getData();

String[] filePathColumn = { MediaStore.Images.Media.DATA };

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

int columnIndex = cursor.getColumnIndex( filePathColumn[0] );
String filePath = cursor.getString( columnIndex );
cursor.close();

// Cursor: /mnt/sdcard/Pic.jpg : /mnt/sdcard/Pic.jpg
U.log( "Cursor: " + filePath + " : " + Uri.parse( filePath ) );

// "regular" 

// Regular: content://media/external/images/media/28 : content://media/external/images/media/28
U.log( "Regular: " + data.getDataString() + " : " + Uri.parse( data.getDataString() ) );

// Regular 2: content://media/external/images/media/28 : content://media/external/images/media/28
U.log( "Regular 2: " + data.getData() + " : " + data.getData() );


mPostImagePreview.setImageBitmap( BitmapFactory.decodeFile( filePath ) );
mPostImagePreview.setVisibility( View.VISIBLE );

他们仍然设置错误的图像。如果我进入图库,请长按图像,然后查看其详细信息:

TItle: Pic
Time: May 2, 2012
Width: 720
Height: 1280
Orientation: 0
File size: 757KB
Maker: Abso Camera
Model: Inspire 4G
Path: /mnt/sdcard/Pic.jpg

因此,图库告诉我路径与拾取操作相同,并且图库正确渲染它。那么,如果我从onActivityResult设置它,它为什么不渲染?

此外,这是我现在用于解雇Intent的代码:

private void selectPhoto()
{
  Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
  intent.setType( "image/*" );
  ( ( Activity )mContext ).startActivityForResult( Intent.createChooser( intent, "Select Picture" ), PHOTO_GALLERY );
}

5 个答案:

答案 0 :(得分:5)

这是打开图库的代码。然而,这与你所做的一样。另请参阅我用于检索所选图像的onActivityResult代码。

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),  
                       PHOTO_GALLERY);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
    case PHOTO_GALLERY:
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = Uri.parse(data.getDataString());
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(  
                                   getApplicationContext().getContentResolver(),   
                                   selectedImageUri);

                this.postImagePreview.setImageBitmap( bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }
}

答案 1 :(得分:4)

有时,图库应用中的缩略图可能会过时并显示不同图像的缩略图。重复使用图像ID时会发生这种情况,例如,当图像被删除并使用相同的ID添加新图像时。

管理应用>图库>清除数据可以解决这个问题。

答案 2 :(得分:3)

private static int RESULT_LOAD_IMAGE = 1;

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

OnActivityResult

  @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                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);
                cursor.close();
                ImageView imageView = (ImageView) findViewById(R.id.imgView);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }
        }

答案 3 :(得分:3)

尝试这个

 //Put this code on some event 

   Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

 startActivityForResult(intent, REQUEST_CODE);

           // When above event fire then its comes to this 
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
              if (resultCode==RESULT_OK && requestCode==1){

                  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]);
                    filePath = cursor.getString(columnIndex);
                    cursor.close();

                            // Use it as per recruitment 
                    actualBitmap =BitmapFactory.decodeFile(filePath);

              }
    }

答案 4 :(得分:0)

试试这个,

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

}