16

我开始请求图像选择:

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

所以,Gallery 告诉我路径与选择动作相同,Gallery 正在正确渲染它。那么,如果我从 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 );
}
4

5 回答 5

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;
        }
    }
}
于 2012-05-12T05:22:24.323 回答
5

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

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

于 2012-05-21T22:12:19.513 回答
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);

活动结果

  @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));
            }
        }
于 2012-05-17T18:43:35.277 回答
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);

              }
    }
于 2012-05-18T06:03:02.503 回答
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
            }
    }
}

}
于 2012-05-18T07:01:49.567 回答