Imagebutton.setImageURI(uri) 不起作用
Imagebutton.setImageURI(uri) doesn't work
现在我的问题是:
这是 Onclcick 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();
}
}
}
试试这个
Uri imgUri=Uri.parse("android.resource://my.package.name/"+R.drawable.image);
mImageSelect.setImageURI(null);
mImageSelect.setImageURI(imgUri);
这是刷新 ImageButton 的解决方法,它尝试缓存以前的图像 Uri。传递 null 有效地重置它。
Imagebutton.setImageURI(uri) doesn't work
使用Glide加载图片更方便
将此添加到您的 build.gradle
compile 'com.github.bumptech.glide:glide.3.7.0'
使用glide加载图片uri如下
Glide.with(MainActivity.this).load(uri).into(imageView);
尝试
mImageSelect.setImageURI(Uri.parse(data.getData());
尝试从 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);
即使fileUri
准备好了,setImageUri()
也不能保证设置好图片。所以在处理图像时总是使用第三方库是一个好习惯。
使用 Picasso 库代替 setImageUri()。
Picasso.with(MainActivity.this).load(fileUri).into(mImageView)
添加为依赖项
compile 'com.squareup.picasso:picasso:2.5.2'
现在我的问题是: 这是 Onclcick 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();
}
}
}
试试这个
Uri imgUri=Uri.parse("android.resource://my.package.name/"+R.drawable.image);
mImageSelect.setImageURI(null);
mImageSelect.setImageURI(imgUri);
这是刷新 ImageButton 的解决方法,它尝试缓存以前的图像 Uri。传递 null 有效地重置它。
Imagebutton.setImageURI(uri) doesn't work
使用Glide加载图片更方便
将此添加到您的 build.gradle
compile 'com.github.bumptech.glide:glide.3.7.0'
使用glide加载图片uri如下
Glide.with(MainActivity.this).load(uri).into(imageView);
尝试
mImageSelect.setImageURI(Uri.parse(data.getData());
尝试从 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);
即使fileUri
准备好了,setImageUri()
也不能保证设置好图片。所以在处理图像时总是使用第三方库是一个好习惯。
使用 Picasso 库代替 setImageUri()。
Picasso.with(MainActivity.this).load(fileUri).into(mImageView)
添加为依赖项
compile 'com.squareup.picasso:picasso:2.5.2'