我尝试过使用 Picasso 以及我在 stackoverflow 上可以找到的所有方法。我正在用相机拍摄图像并获取 onActivity 结果的值。仅当我退出并重新启动应用程序后,imageView 才会更新。我希望 imageview 在我眼前改变。所有代码似乎都不起作用。
我试过了
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
profile.setImageBitmap(thumbnail);
}
});
点击“确定”后相机 View 关闭时没有任何反应。
我也尝试过这个:
Thread thread = new Thread()
{
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
profile.setImageBitmap(thumbnail);
}
});
}
};
thread.start();
但 UI 没有改变,图像保持不变。
我尝试了异步任务,Picasso 也不起作用。
不知道该怎么做,已经有一段时间了,我非常沮丧
这是使用这两种技术的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && data != null) {
final Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
final File destination = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "temp.jpg");
FileOutputStream fo;
try {
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
uploadProfilePicToS3(destination.getAbsolutePath());
//this doesn't work
Thread thread = new Thread()
{
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
profile.setImageBitmap(thumbnail);
}
});
}
};
thread.start();
//not working either!
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
profile.setImageBitmap(thumbnail);
}
});
最佳答案
ImageView
未更新的原因是 1) 只要您将图像保存到同一目标,Android 就会加载缓存中的上一张图像 2) onActivityResult
在 onResume
之前调用。这意味着当调用 onResume
时,加载到 ImageView
中的图像将会丢失。
第一个问题可以通过Glide
的signature
很好的解决,第二个问题可以通过onSavedInstanceState()
解决。
更改图像时创建Glide 签名
。每次更改都应该有所不同,例如当前时间。然后在加载图像时应用签名
。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && data != null) {
final Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
final File destination = new File(
Environment.getExternalStorageDirectory()
.getAbsolutePath(), "temp.jpg");
FileOutputStream fo;
try {
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.flush();
} catch (IOException e) {
e.printStackTrace();
}
Uri uri = Uri.fromFile(destination)
//Create your signature
SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss",
Locale.getDefault());
String timeStamp = dateFormat.format(new Date());
newImageSignature = new RequestOptions()
.signature(new ObjectKey(timeStamp));
//Load your image
Glide.with(yourActivity.this) // Or Fragment
.load(bitmap) // Or Uri
.apply(newImageSignature)
.into(imageView);
// You can save the uri and signature to Firebase Database,
// and your image to Storage here
然后将新的签名
保存在onSaveInstanceState()
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (newImageSignature != null) {
outState.putString("IMAGE_SIGNATURE”, newImageSignature.toString());
outState.putString("IMAGE_URI”, uri.toString());
}
}
现在您可以在 onCreate()
中检索签名
Uri ImageUri;
ImageView imageView;
RequestOptions imageSignature;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve your saved parameters. Or get them from a database.
if (savedInstanceState != null){
imageSignature = new RequestOptions()
.signature(new
ObjectKey(savedInstanceState.getString("IMAGE_SIGNATURE")));
imageUri = Uri.parse(savedInstanceState.getString("IMAGE_URI"));
// Instantiate your imageview
// Load image
if(imageUri != null && imageSignature != null){
Glide.with(yourActivity.this) // Or Fragment
.load(bitmap) // Or Uri
.apply(newImageSignature)
.into(imageView);
}
}
注意:我刚刚写了这个,尚未测试。希望它能提供
关于java - ImageView 未根据 onActivityResult 中的更改进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41170255/