Sorry for my english is not good.
I have a problem, i download image from url by using picasso library, then store it in sdcard, but i can't see it in sdcard, and i only see it when i restart my phone, here is my code, what i do wrong??? help me, please !!!
public class Image extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_show_image);
Intent it = getIntent();
Bundle bundle = it.getExtras();
final String link = bundle.getString("link");
final ImageView ivView = (ImageView) findViewById(R.id.ivView);
Picasso.with(getApplicationContext()).load(link).into(ivView);
Button btSetWallpaper = (Button) findViewById(R.id.btSetWallpaper);
btSetWallpaper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
downloadImage(link);
}
});
}
public void downloadImage(final String link){
Picasso.with(getApplicationContext()).load(link).into(new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/abc.jpg");
try {
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
}
catch (Exception e){
e.getStackTrace();
}
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Success" + Environment.getExternalStorageDirectory().getPath(), Toast.LENGTH_LONG).show();
}
#Override
public void onBitmapFailed(Drawable drawable) {
Toast.makeText(getApplicationContext(), "Load Fail", Toast.LENGTH_LONG).show();
}
#Override
public void onPrepareLoad(Drawable drawable) {
}
});
}
}
You need to tell Android Media Library that a media file has been added.
Intent intent =
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
Replace the file with your saved file and it should appear in your Android Media Library without rebooting the phone.
You need to tell the system to run a media scanner and only then the file will appear in the gallery. Which can be done as follows:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
OR
MediaScannerConnection.scanFile(this,
new String[] { filepath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// your code here
}
});
Related
i want to share an image from url using picasso library,first load the bitmap then share the image with a text, it works but in some cases it doesn't work and i get this message
W/System.err: remove failed: ENOENT (No such file or directory) : /data/data/is.com.raisse.fortniteguide/cache/picasso-cache/journal.tmp
i don't know the cause of this message
this is my code
myViewHolder.shareImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myViewHolder.progressBar.setVisibility(View.VISIBLE);
#SuppressLint("HandlerLeak") final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
Picasso.get().load(newsLst.get(position).getImgUri()).into(new Target() {
#Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_TEXT, newsLst.get(position).getUrl());
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
myViewHolder.progressBar.setVisibility(View.GONE);
context.startActivity(Intent.createChooser(i, "Share Image"));
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
myViewHolder.progressBar.setVisibility(View.GONE);
Log.d("Picasso","===========> Error"+e.getMessage());
}
#Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
});
super.handleMessage(msg);
}
};
new Thread(new Runnable() {
#Override
public void run() {
handler.sendEmptyMessage(1);
}
}).start();
}
});
getLocalBitmapUri method:
private Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
I have a chat app and I need to save the images that the user sends receives. For the images the user sends I am saving it to an image folder like so
private void imageDownload(final String url){
Picasso.with(getContext())
.load(url)
.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 heyJudeFile = new File(Environment.getExternalStorageDirectory().getPath() + "/Hey Jude");
if (!heyJudeFile.exists()){
heyJudeFile.mkdirs();
}
localProfilePictureAdress = Environment.getExternalStorageDirectory().getPath() + "/Hey Jude" + "/" + url.substring(url.lastIndexOf("/")+1);
File file = new File(localProfilePictureAdress);
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;
}
But for images the user receives I want to save the images but I do not want the images to be viewable in the gallery how does that work?
Thanks
Use below method to hide media from gallery
/* To Hide media file in gallery */
public void createNoMedia(String myDir){
File noMediaFile = new File(myDir, ".nomedia");
if (!noMediaFile.exists()) {
try {
noMediaFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* To use this */
dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/MyImages/";
File newDir = new File(dir);
newDir.mkdirs();
createNoMedia(dir);
and in manifest.xml
You also need the WRITE_EXTERNAL_STORAGE permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
you can do it by editing name of your file and adding an unknown format like ".example" then it doesn't appear in gallery
I'm trying to share image on Instagram in my app. I've url of the image and using Picasso library to download image.
Target target = new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d(TAG, "Bitmap Loaded");
File outputDir = getApplicationContext().getCacheDir(); // context being the Activity pointer
try {
File outputFile = File.createTempFile("instagram", "png", outputDir);
outputFile.createNewFile();
FileOutputStream ostream = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
ostream.close();
Log.d(TAG, "Image downloaded");
shareOnInstagram(Uri.fromFile(outputFile));
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(this).load(imageUrl).into(target);
But onBitmapLoaded is never being called. Is there any other way to share image on Instagram from a url? The intent which share on Instagram takes Intent.EXTRA_STREAM parameter which should be a media path on device.
How do I convert an image from a url into that type?
Picasso only keeps weak reference to target, so in your case it will be garbage collected. As a result, onBitmapLoaded is not being called.
You should store strong reference to target (make target member of your class).
I using this approach
public class ShareToOtherApp extends AsyncTask<Bitmap, Void, Uri> {
#Override
protected Uri doInBackground(Bitmap... bitmaps) {
return bitmaps.length > 0 ? BitmaptoUri(bitmaps[0]) : null;
}
#Override
protected void onPostExecute(Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
if (uri != null) {
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
}
shareIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(shareIntent, "Share Image");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyApp.GetContext().startActivity(chooserIntent);
}
public File GetSDCardDir(){
boolean ISSDCard;
File[] Dirs = ContextCompat.getExternalFilesDirs(MyApp.GetContext(), null);
ISSDCard = false;
for (File Dir : Dirs) {
if (Dir != null) {
if (Dir.getPath().contains("sdcard")) {
ISSDCard = true;
break;
}
}
}
File SDCardDir;
if(ISSDCard && Dirs[Dirs.length -1] != null){
SDCardDir = Dirs[Dirs.length -1];
}else{
SDCardDir = Dirs[0];
}
return SDCardDir;
}
public Uri BitmaptoUri(Bitmap bitmap){
Uri uri = null;
try {
File file = new File(GetSDCardDir() , HConstants.IMG_FILE_NAME + ".jpg");
if(file.getParentFile() != null){
file.getParentFile().mkdirs();
}else{
GetSDCardDir().mkdirs();
}
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
uri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return uri;
}
}
and finally for using from it.
new ShareToOtherApp().execute(bitmap);
I have a button that downloads an image to the phone gallery using Picasso library.. the image is downloaded but I can't save it to the gallery..
I'm trying to save the image to the DCIM folder using the following code :
if (bitmap != null) {
FileOutputStream out = null;
try {
String name = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
+ "/"
+ pageTitle
+ "-img"
+ System.currentTimeMillis() + "." + imgExt;
Log.d("Bitmap", "Name: " + name);
File f = new File(name);
out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
Intent mediaScanIntent = new
Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
SinglePageActivity.this.sendBroadcast(mediaScanIntent);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Log.d("Bitmap", "Null image");
}
But I get the following exception :
java.io.FileNotFoundException:
/storage/sdcard/DCIM/offer11-img1438046842226.jpg: open failed: EACCES
(Permission denied)
Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
on this line :
out = new FileOutputStream(f);
Also is there a more efficient way to save the image to the gallery directly ?
Thanks
check if you have declared the permission in
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>,
if not,then try to add;
You can use like this
public class ImageDownload implements Target {
public static final String TAG = "tag";
private String name;
public ImageDownload(String fileName) {
name = fileName.substring(fileName.lastIndexOf("/"));
}
#Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void onBitmapLoaded(final Bitmap bitmap, LoadedFrom arg1) {
Log.e("ImageDownlaod", "inside Download ");
new Thread(new Runnable() {
#Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + "/" + FACTOR_APP + "/" + name);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
#Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub
}
}
and you can use this class as
public void downLoadImagesWithPicasso() {
Picasso.with(this).load(thumbnailPath)
.into(new ImageDownload(thumbnailPath));
}
I'm trying to download a mp3 file generated with google translate. It works fine with languages with Latin script, but if I for example try Thai it dosen't work at all.
Any one have a suggestion? This is my code:
public class MyActivity extends Activity {
private Button mButton;
private File mOutputFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
new Thread(new Runnable() {
#Override
public void run() {
String uri = Uri.parse("http://translate.google.com/translate_tts")
.buildUpon()
.appendQueryParameter("tl", "th")
.appendQueryParameter("q", "ก")
.build().toString();
mOutputFile = new File(getFilesDir(), "sound.mp3");
try {
new DefaultHttpClient().execute(new HttpGet(uri))
.getEntity().writeTo(
new FileOutputStream(mOutputFile));
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
mButton = (Button) findViewById(R.id.speak);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play(mOutputFile.getAbsolutePath());
}
});
}
private void play(String path) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I found out awhile ago and remember I should post my findings. I needed to add another parmeter to the url:
.appendQueryParameter("ie", "utf-8")
Now it works perfect.