웹 서비스에서 제공하는 URL에서 이미지를 저장 한 다음 일부 활동에서 해당 이미지를 검색하고 사용해야합니다.
이미지를 다운로드하고 캐시하기 위해 이미 Picasso를 사용하고 있었으므로 다음 방법을 사용했습니다.
public void downloadFile(String url) {
Picasso.with(AnimationActivity.this).load( url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/bBackground");
if (!myDir.exists()) {
myDir.mkdirs();
}
String name = "bBg.jpg";
myDir = new File(myDir, name);
FileOutputStream out = new FileOutputStream(myDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch(Exception e){}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
});
}
그런 다음 이미지를 검색하기 위해 다음 코드로 시도했습니다.
String bgImagePath = Environment.getExternalStorageDirectory() + "/bBackground/bBg.jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(bgImagePath, options);
ImageView imageView = (ImageView) findViewById(R.id.bBgImage);
imageView.setImageBitmap(mustOpen);
하지만 이미지를 검색 할 때 오류가 발생합니다.
E/BitmapFactory(30761): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/bBackground/bBg.jpg: open failed: ENOENT (No such file or directory)
내가 무엇을 잘못하고 있지? 저장 또는 검색 방법과 관련된 문제입니까?
매니페스트 파일에 다음 권한을 추가 했습니까?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
이미지를 저장하려면 다음 기능을 사용하십시오.
public void saveBitmap(Bitmap bitmap) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/bBackground");
if (!dir.exists()) {
dir.mkdirs();
}
File image = new File(dir, "/bBg.jpg");
try {
FileOutputStream out = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
}
이 이미지를 읽으려면 다음을 사용할 수 있습니다.
public Bitmap getBitmap(){
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/bBackground");
File image = new File(dir, "/bBg.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(image, options);
ImageView imageView = (ImageView) findViewById(R.id.bofrostBgImage);
imageView.setImageBitmap(mustOpen);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다