ImageButton.setImageURI(uri)无法正常工作。

3

现在我的问题是:

这是一个针对点击ImageButton的解决方法,它试图缓存先前的图像Uri。传递null可以有效地重置它。

private ImageButton mImageSelect;
private static  final int GALLERY_REQUEST =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

    mImageSelect=(ImageButton) findViewById(R.id.imageselect);
    mImageSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent= new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent, GALLERY_REQUEST);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   // super.onActivityResult(requestCode, resultCode, data);

    if(requestCode== GALLERY_REQUEST && resultCode==RESULT_OK){
        Uri selectedImageUri=data.getData();
        if (null != selectedImageUri) {

            mImageSelect.setImageURI(selectedImageUri);
            Toast.makeText(PostActivity.this, "Image Selected", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(PostActivity.this,"Image Not Selected",Toast.LENGTH_SHORT).show();
        }
    }
}

你确切想要问什么? - Mike M.
你不能直接这样设置图片。 - g7pro
Mike,我想问一下如何从图库中设置ImageButton的图像。但是setImageUri不起作用。 - Bilal Malik
5个回答

1
即使fileUri已经准备好了,setImageUri()也不能保证设置图片。因此,在处理图像时,始终使用第三方库是一个好习惯。 使用Picasso库而不是setImageUri()。
Picasso.with(MainActivity.this).load(fileUri).into(mImageView)

添加为依赖项。
compile 'com.squareup.picasso:picasso:2.5.2'

0

试试这个

Uri imgUri=Uri.parse("android.resource://my.package.name/"+R.drawable.image);
mImageSelect.setImageURI(null); 
mImageSelect.setImageURI(imgUri);

这是解决刷新ImageButton的方法,它尝试缓存先前的图像URI。传递null有效地重置它。


0
不起作用。
您可以使用 Glide 更轻松地加载图像。
将以下内容添加到您的 build.gradle 文件中。
  compile 'com.github.bumptech.glide:glide.3.7.0'

使用Glide加载图像URI的代码如下:
 Glide.with(MainActivity.this).load(uri).into(imageView); 

0

尝试

    mImageSelect.setImageURI(Uri.parse(data.getData());

0
尝试从URI创建位图并将其设置为图像按钮。 可以这样做:
String path = getPath(this,uri);

getPath()的代码:

public static String getPath(final Context context, final Uri uri) {
    Utils.context = context;
    Utils.uri = uri;

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] {
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

现在可以获取位图:

    if(path!=null)
        Bitmap bm = getBitmapFromPath(path)

将图像设置为imageButton:

ImageButton.setImageBitmap(bm);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接