I have tried using Picasso and every method I could possibly find here on stackoverflow. I'm taking an image with camera and getting the value onActivity result. The imageView gets updated only after I quit and restart the application. I want imageview to change before my eyes. None of the code seems to be working.
I tried this
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
profile.setImageBitmap(thumbnail);
}
});
nothing happens when camera view closes after hitting ok.
I also tried this:
Thread thread = new Thread()
{
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
profile.setImageBitmap(thumbnail);
}
});
}
};
thread.start();
but UI doesn't change, image remains the same.
I tried Async tasks, Picasso doesn't work either.
Idk how to do this, it's been a while and I'm super frustrated
Here is the code with both techniques:
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);
}
});
The reason your ImageView
is not updated is because 1) Android loads the previous image that is in the cache, as long as you're saving the image to the same destination 2) onActivityResult
is called before onResume
. This means that the image you loaded into the ImageView
is lost when onResume
is called.
The first problem is nicely handled by Glide
's signature
, and the second problem can be handled with onSavedInstanceState()
.
Create a Glide signature
when you change the image. This should be different for each change eg current time. Then apply the signature
as you load the image.
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
Then save the new signature
in 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());
}
}
Now you can retrieve the signature
in 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);
}
}
NB: I just wrote this, not tested yet. Hope it gives
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.