I have a problem when saving image from web.
I create new folder and save all images to this. The problem is I want this folder order by the last download image on first but When i open gallery, the folder is order by the image date created and the date created in the downloaded image is not the time I download but the first time it created. I've already search on stack over flow and see that java can't modified the date created in image to the time I download it.
Any one has the solution? (Sorry for the bad English)
Thanks you for the comment. I will explain more details.
First, I download image from web to cache directory
HttpURLConnection localHttpURLConnection = (HttpURLConnection) new java.net.URL(urldisplay).openConnection();
localHttpURLConnection.setConnectTimeout(30000);
localHttpURLConnection.setReadTimeout(30000);
localHttpURLConnection.setInstanceFollowRedirects(true);
InputStream in = localHttpURLConnection.getInputStream();
File localFile = Constans.fileCache.getCacheFile(urldisplay);
FileOutputStream fos = new FileOutputStream(localFile);
Utils.CopyStream(in, fos); // simple copy by trunks
fos.close();
Second, I copy downloaded image to external storage
File toFile = new File(Environment.getExternalStorageDirectory() + "/folder", "folder_" + System.currentTimeMillis() + ".png");
FileOutputStream fos = new FileOutputStream(toFile);
Utils.CopyStream(new FileInputStream(fromFile), fos);
fos.close();
// Scan image to display when open with gallery otherwise it couldn't see in gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(toFie);
mediaScanIntent.setData(contentUri);
mContext.sendBroadcast(mediaScanIntent);
Lastly, I see that gallery don't sort my image by the time I downloaded. That is the problem i want to fix.
Not sure I understood, but let's try.
First the issue you mention is more specific to Gallery application than a java code issue.
I assume Gallery use EXIF information to order the picture by date they were taken, not oder they are downloaded/copied. Unfortunatly Gallery does not provide any option to sort picture in other oders.
Maybe you can try to use another explorer that allows you to sort the pictures in another order (maybe ESFileExplore which has more options?)
Ultimate solution: you can try to change EXIF in your pictures using a java EXIF library to modify picture taken date and this should change the order they appear in the Galery (but very ugly solution...). Some random EXIF libraries after 5 seconds of Google:
http://drewnoakes.com/code/exif/
http://www.toanthang.net/modules.php?name=News&new_topic=2&catid=7
Hope this helps
Thierry
Related
I was trying to upload an image in the facebook however, when it is about to be posted the image gets pixelized but when the post has been successful the image if not pixelized and it is the right image. How can I fix image that will be sent to facebook's upload picture?
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), img2[res2]);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg";
OutputStream out = null;
File file=new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
Intent shareIntent = new Intent();
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/jpg");
startActivity(Intent.createChooser(shareIntent,"Share with"));
I was trying to upload an image in the facebook however, when it is about to be posted the image gets pixelized but when the post has been successful the image if not pixelized and it is the right image. How can I fix image that will be sent to facebook's upload picture?
The image get pixelized by Facebook when you are posting the image? There is nothing wrong with that, that's how they designed it to work. There is nothing you can do from your end
Just do like this send bitmap directory using intent
Bitmap photo = BitmapFactory.decodeFile(picturePath);
Bitmap imageBitmap= ReSizeBitmap.getResizedBitmap(photo, 300, 300);
And send bitmap object Directly using shareIntent.putParceble(imageBitmap)
It seems like you have a resource ID (of an image file), that you are trying to get the image file from.
resources.getResourceName(ID) takes an ID and returns a string representation of the filename.
Let us use this information. Since, we need have the resource ID and need to get the URI,
Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
context.getResources().getResourcePackageName(resourceID) + '/' +
context.getResources().getResourceTypeName(resourceID) + '/' +
context.getResources().getResourceEntryName(resourceID) );
should give the URI. There might be small corrections needed in places. Please feel free to edit my answer to reflect the same.
No compression required. Yay!
Did that help?
Do not process the image. Right now you are decoding the image, then re-encoding it as a JPEG.
Instead, use openRawResource() on Resources() to get an InputStream on the resource, then copy that content to a FileOutputStream using ordinary Java file I/O. Be sure to flush(), getFD().sync(), and close() the FileOutputStream when you are done.
Then, make sure that the MIME type in the shareIntent matches the actual MIME type of the drawable resource. If your resource is a JPEG, what you have now will be fine.
Also:
Do not use concatenation to create a file path. Use File constructors. This ensures that you handle directory separators properly.
Use Uri.fromFile() to convert a File to a Uri, not string concatenation.
Unless you think that the user wants this image to be alongside the rest of their photos, do not use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES). Instead, use getExternalCacheDir() (available on Context), so you do not clutter up the user's gallery, etc.
However, in the end, your bitmap quality is only partially controlled by you. The app that you are sending it to is welcome to do whatever it wants with the bitmap, and "whatever it wants" may affect the quality. There is nothing you can do about this, other than file bug reports with the other app's authors.
Another, more complex possibility is for you to implement a streaming ContentProvider, to save you from writing the data to a file on external storage and therefore expecting third-party apps to have READ_EXTERNAL_STORAGE as a permission.
After searching about "How to save Layout views as images", I've found some solution to save in Internal and External Storage. But It seems the image file created is going to save in some data/data/... folder that is not visible normally. Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not:
View content = findViewById(R.id.relativeLayout);
String yourimagename = "MyImageFile";
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/" + yourimagename + ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
ostream.close();
content.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
But It seems the image file created is going to save in some data/data/... folder that is not visible normally.
The file will be saved where the programmer elects to save it.
Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not
That code will not work on any version of Android, as new File("/" + yourimagename + ".png") is not going to give you a usable File, as it points to a place that you can neither read nor write.
You are welcome to save the image to internal storage or external storage. Since you want this image to be picked up by "gallery"-type apps, you are best off choosing external storage, then using MediaScannerConnection and its scanFile() method to get the file indexed by the MediaStore, since gallery apps will tend to use the MediaStore as their source of images.
On the whole, I worry that getDrawingCache() will be unreliable. You may be better served telling your root View to draw to your own Bitmap-backed Canvas instead.
I want to copy a remote image, for example "http://example.com/example.jpg" to the android user phone built gallery...How can I do it?
To that, you should download the image and save it in internal memory.
You can download the image by yourself:
public static Bitmap getBitmap(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);
is.close();
return d;
} catch (Exception e) {
return null;
}
}
Code from here But you will have memory problems with large images. I strongly recommended you to use a build library like Android Universal Image Loader or Picasso from square
Here you can find an example of how to use the Android DownloadManager to download your file.
The destination path can be determined using the contants defined in the Environment class. Constant DIRECTORY_DCIM points to the parent directory under which all Activities can create a custom folder where they store their images. You could make your own child folder as destination folder
When your image finishes downloading, you will notice that it will not be listed in the default gallery application, this is because Android builds an index with all the media files and is still unaware of your new downloaded image. This index is updated each time you boot your Android device, but since it's a bit unconvienient to reboot your device each time a file is added, you can also codewise inform the indexing service that a new file is created and needs indexing using this piece of code:
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
This scanning should also occur after a file has been erased.
I am building a multilevel navigation drawer in my application where I would need to cache some 70-80 images which I do not want to download in front of users. Right now, I am using picasso image library which allows me to load images once the app gets started.
Load:
Picasso.with(getActionBar().getThemedContext()).load(imageURL);
Setting image in view:
Picasso.with(_context).load(imageURL).into(ImageView);
I want to store the images in user's phone so that the drawer could show them up even when the user is offline. I am not sure if I should use internal or external storage(also how would I use). Is there a way I could store these images in phone using picasso somehow because the effects picasso has for image loading seems good.
If not, how should I go about storing my images? I cannot hardcode this because these images would change every 2-4 days.
You can save the images like this:
File sdcard = Environment.getExternalStorageDirectory();
File f = new File (sdcard, "filename.png");
FileOutputStream out = new FileOutputStream(f);
yourBitmap.compress(Bitmap.CompressFormat.PNG, 90, out)
hi i have a problem with a bitmap and a picture located here:
file:///mnt/sdcard/Android/data/com.dev.app/files/Pictures/20120924-092226.jpg
so i have a string with this url (the the file exists!)
now i need to get a bitmap, so i do:
String str = "file:///mnt/sdcard/Android/data/com.dev.app/files/Pictures/20120924-092226.jpg";
Bitmap b = BitmapFactory.decodeFile(str);
but b is always null. and i dont know why.
EDIT: i create the file before with a code like this:
String filename = DateFormat.format("yyyyMMdd-hhmmss",Calendar.getInstance().getTime()) + ".jpg";
Uri imageUri = helper.createImageDestinationUri(null, filename);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, requestCode);
There are two possibilities
The file is of incorrect format, i.e although the extension is .jpg its not a jpeg file.
The file cannot be loaded.
You may have a closer look at the adb logs to see what is going on ... if it does not helps , try the following.
Add the file in your drawable folder, and use it in some Test screen. If the file gets displayed then the file format is correct , so android is not able to load the file. Check your permissions, and actually try and read the file first using FileInputStream or somthing else.
If the file is still not displayed then you can be sure that the format is wrong. In that case just try using a different image file.
Hope it helps...