i'm trying to save a image when an ImageView is clicked. Because the saving takes some time i want to have some sort of indicator, that the app is working. I tried to use Snackbar for that. My code looks something like this:
File image = new File(directory, "image.png");
if(!image.exists()){
Snackbar bar = Snackbar.make(getActivity().findViewById(R.id.some_layout), "snackText", Snackbar.LENGTH_INDEFINITE);
bar.show();
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
bar.dismiss();
}
All that is inside the public void onClick(View view)
of that ImageView. The problem is, that the Snackbar only shows after the image is saved. I tried to force update the UI with invalidate() and tried it without starting a new Thread with the same result. Any ideas are greatly appreciated!
Don't use snackbar here edit your code like this: if(!image.exists()){
final ProgressDialog progressDialog =ProgressDialog.show(this, "","Please Wait...", true);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
});
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
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.