I'm trying save an image using API Picasso. To do it I'm trying use Target
to save but I can't do this work.
我正在尝试使用API Picasso保存图像。要做到这一点,我正在尝试使用Target保存,但我不能做这项工作。
How could I do this ?
我怎么能这样做?
Trying
//save image
public static void imageDownload(Context ctx){
Picasso.with(ctx)
.load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
.into(getTarget("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png"));
}
//target to save
private static Target getTarget(final String url){
Target target = new Target(){
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
//Log.i("PRODUTOS_FOLDER", CreateAppFolder.getProdutosFolder());
File file = new File(Environment.getExternalStorageDirectory() + url);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.flush();
ostream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
return target;
}
Exception
java.io.IOException: open failed: ENOENT (No such file or directory)
4 个解决方案
#1
28
Solved. now works fine!
解决了。现在工作正常!
I did
//save image
public static void imageDownload(Context ctx, String url){
Picasso.with(ctx)
.load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
.into(getTarget(url));
}
//target to save
private static Target getTarget(final String url){
Target target = new Target(){
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
Log.e("IOException", e.getLocalizedMessage());
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
return target;
}
#2
3
I can see 2 possible issues:
我可以看到两个可能的问题:
- trying to save to external storage without write permissions in your manifest
- try change the filename so its not the whole url, which could be your issue because of the characters in your url that arent valid as filename chars.
尝试在清单中没有写入权限的情况下保存到外部存储
尝试更改文件名,使其不是整个网址,这可能是你的问题,因为你的网址中的字符不能作为文件名字符有效。
#3
1
I Modified the solution to this, adding permissions and a button to load and save the Image,and PhotoLoader class remains the same !
我修改了解决方案,添加权限和一个按钮来加载和保存图像,PhotoLoader类保持不变!
private static final String[] STORAGE_PERMISSIONS = { Manifest.permission.WRITE_EXTERNAL_STORAGE};
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
verifyPermissions();
}
public void save(View view)
{
Picasso.with(this)
.load("https://www.w3schools.com/howto/img_fjords.jpg")
.into(new PhotoLoader("myImg.jpg" , imageView));
}
public void verifyPermissions()
{
// This will return the current Status
int permissionExternalMemory = ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permissionExternalMemory != PackageManager.PERMISSION_GRANTED)
{
// If permission not granted then ask for permission real time.
ActivityCompat.requestPermissions(MainActivity.this,STORAGE_PERMISSIONS,1);
}
}
#4
0
I think you need to check whether you are actually requesting the permission. In Android the permissions are dynamic starting from the version 6.0. Either you must request it at run time or just downgrade your targetSdk version to 22.
我想你需要检查一下你是否真的要求获得许可。在Android中,权限是从6.0版开始的动态。您必须在运行时请求它或者只是将targetSdk版本降级到22。
#1
28
Solved. now works fine!
解决了。现在工作正常!
I did
//save image
public static void imageDownload(Context ctx, String url){
Picasso.with(ctx)
.load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
.into(getTarget(url));
}
//target to save
private static Target getTarget(final String url){
Target target = new Target(){
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
Log.e("IOException", e.getLocalizedMessage());
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
return target;
}
#2
3
I can see 2 possible issues:
我可以看到两个可能的问题:
- trying to save to external storage without write permissions in your manifest
- try change the filename so its not the whole url, which could be your issue because of the characters in your url that arent valid as filename chars.
尝试在清单中没有写入权限的情况下保存到外部存储
尝试更改文件名,使其不是整个网址,这可能是你的问题,因为你的网址中的字符不能作为文件名字符有效。
#3
1
I Modified the solution to this, adding permissions and a button to load and save the Image,and PhotoLoader class remains the same !
我修改了解决方案,添加权限和一个按钮来加载和保存图像,PhotoLoader类保持不变!
private static final String[] STORAGE_PERMISSIONS = { Manifest.permission.WRITE_EXTERNAL_STORAGE};
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
verifyPermissions();
}
public void save(View view)
{
Picasso.with(this)
.load("https://www.w3schools.com/howto/img_fjords.jpg")
.into(new PhotoLoader("myImg.jpg" , imageView));
}
public void verifyPermissions()
{
// This will return the current Status
int permissionExternalMemory = ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permissionExternalMemory != PackageManager.PERMISSION_GRANTED)
{
// If permission not granted then ask for permission real time.
ActivityCompat.requestPermissions(MainActivity.this,STORAGE_PERMISSIONS,1);
}
}
#4
0
I think you need to check whether you are actually requesting the permission. In Android the permissions are dynamic starting from the version 6.0. Either you must request it at run time or just downgrade your targetSdk version to 22.
我想你需要检查一下你是否真的要求获得许可。在Android中,权限是从6.0版开始的动态。您必须在运行时请求它或者只是将targetSdk版本降级到22。