使用位图的毕加索图像缓存
Picasso Image Caching using Bitmap
尽管我在 OnActivityResult.
中调用此方法,但此 loadImage 方法的内容从未被调用
public void loadImage(Uri uri){
File file = new File(getApplicationContext().getExternalCacheDir().getAbsolutePath() + "/" +uid + ".jpg");
Log.d("Check Bitmap", "file" + file);
try {
Picasso picasso = Picasso.get();
Bitmap bitmap = picasso.load(uri).get();
Log.d("Check Bitmap", "bitmap working");
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,new FileOutputStream(file));
} catch (Exception e) {
Log.d("Check Bitmap", "bitmap not working, cached");
e.printStackTrace();
}
我的目标是从文件中检索图像。
这是我的 OnActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode ==1000){
{
if(resultCode == Activity.RESULT_OK){
imageUri = data.getData();
Context context;
final ProgressDialog dialog = new ProgressDialog(myProfile.this);
dialog.setMessage("Uploading Image...");
dialog.show();
profilepic.setImageURI(imageUri);
ref.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
final Picasso picasso = Picasso.get();
picasso.setIndicatorsEnabled(true);
picasso.load(uri).into(profilepic);
downloadUri = uri;
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Image Uploaded!", Toast.LENGTH_SHORT).show();
loadImage(uri); //Here I call
您不应使用 space 分隔的 TAG 作为日志。尝试将 "Check Bitmap"
替换为一个词(例如“Checkere”)。然后您将能够在 Logcat 中看到您的登录。
您需要将所有内容都放在一个线程中。
这是 loadImage
的完整代码块
public void loadImage(final Uri uri){
Thread thread = new Thread() {
@Override
public void run() {
Log.d("Checkere", "stuck at file");
File file = new File(getCacheDir() + File.separator + uid + ".jpg");
Log.d("Checkere", "file" + file);
try {
Picasso picasso = Picasso.get();
Bitmap bitmap = picasso.load(uri).get();
Log.d("Checkere", "bitmap working");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
Log.d("Checkere", e.getMessage());
e.printStackTrace();
}
}};
thread.start();
}
请注意,我还在您的代码中添加了这两行。
fOut.flush();
fOut.close();
希望对您有所帮助!
尽管我在 OnActivityResult.
中调用此方法,但此 loadImage 方法的内容从未被调用public void loadImage(Uri uri){
File file = new File(getApplicationContext().getExternalCacheDir().getAbsolutePath() + "/" +uid + ".jpg");
Log.d("Check Bitmap", "file" + file);
try {
Picasso picasso = Picasso.get();
Bitmap bitmap = picasso.load(uri).get();
Log.d("Check Bitmap", "bitmap working");
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,new FileOutputStream(file));
} catch (Exception e) {
Log.d("Check Bitmap", "bitmap not working, cached");
e.printStackTrace();
}
我的目标是从文件中检索图像。
这是我的 OnActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode ==1000){
{
if(resultCode == Activity.RESULT_OK){
imageUri = data.getData();
Context context;
final ProgressDialog dialog = new ProgressDialog(myProfile.this);
dialog.setMessage("Uploading Image...");
dialog.show();
profilepic.setImageURI(imageUri);
ref.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
final Picasso picasso = Picasso.get();
picasso.setIndicatorsEnabled(true);
picasso.load(uri).into(profilepic);
downloadUri = uri;
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Image Uploaded!", Toast.LENGTH_SHORT).show();
loadImage(uri); //Here I call
您不应使用 space 分隔的 TAG 作为日志。尝试将 "Check Bitmap"
替换为一个词(例如“Checkere”)。然后您将能够在 Logcat 中看到您的登录。
您需要将所有内容都放在一个线程中。
这是 loadImage
的完整代码块 public void loadImage(final Uri uri){
Thread thread = new Thread() {
@Override
public void run() {
Log.d("Checkere", "stuck at file");
File file = new File(getCacheDir() + File.separator + uid + ".jpg");
Log.d("Checkere", "file" + file);
try {
Picasso picasso = Picasso.get();
Bitmap bitmap = picasso.load(uri).get();
Log.d("Checkere", "bitmap working");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
Log.d("Checkere", e.getMessage());
e.printStackTrace();
}
}};
thread.start();
}
请注意,我还在您的代码中添加了这两行。
fOut.flush();
fOut.close();
希望对您有所帮助!