Displaying image from sd card - android

I use this code
String imgFile = Environment.getExternalStorageDirectory() + "/p1.png";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
image.setImageBitmap(myBitmap);
but imageview's background isn't p1.png there is no image at the background it is white please help.Thanks..

You should use the full path of the SDCard. Then you can do normal I/O-Operations just like this:
String imgFile = String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "p1.png";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
image.setImageBitmap(myBitmap);r code here

Related

How to compress image without losing its Metadata?

I need to compress images before sending the images to the server via Android app. The images taken via camera are very large and they need to be compressed first. When a image is taken from Camera, there are some properties associated with the image like device model, aperture, location, etc.
I'm trying to compress the image, but during compression, those properties of the images are lost. Please help if there is any missing line of code or any library to compress the images without losing those properties.
private void compressImageAndAddToArrayList(Uri imageURI) {
Bitmap bm = null;
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
// bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
bm = BitmapFactory.decodeStream(mContext.getContentResolver().openInputStream(imageURI), null, options);
File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "VDMS" + File.separator + "Camera");
folder.mkdirs();
String now = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()).toString();
String fileName = caseID + "_" + now + ".jpg";
File imageFile = new File(folder + File.separator + fileName);
FileOutputStream out = new FileOutputStream(imageFile);
bm.compress(Bitmap.CompressFormat.JPEG, 64, out); // quality ranges from 0-100
bm.recycle();
addDocumentToArrayList(String.valueOf(1), Uri.fromFile(imageFile));
}
catch (Exception e)
{
e.printStackTrace();
}
}

webview saves same screenshot again android

The below code captures the webview image and save it in Pictures folder. The code is as follows
public static void getBitmapOfWebView(final WebView webView){
Date now = new Date();
String mPath = Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_PICTURES + File.separator + "Screenshot_" + now + ".jpg";
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache(true);
Picture picture = webView.capturePicture();
Bitmap b = Bitmap.createBitmap( webView.getWidth(), webView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mPath);
if ( fos != null ) {
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
}
}
catch(Exception e) {}
b.recycle();
}
The above code works and saves the screenshot in the pictures folder. But the problem is when i scroll down the WebView and try to execute the same code again, it saves the old screenshot of the webview in the pictures folder.
How do i code in such a way that when i scroll the Webview, it should save the current screenshot of the webview.
Any help Appreciated.
Thank you.

Android: Insert Bitmap to Images.Media always get black background to stored image

I am inserting the image using InsertImage, but every time when image is stored on sd card its background become black. How can I remove that black background?
My code is:
> Bitmap Img = BitmapFactory.decodeResource(getResources(),
> R.drawable.ic_launcher); String path =
> Images.Media.insertImage(getContentResolver(), Img, "myImg", "Image");
please use this format before saving images in SD card ----->>> Bitmap.CompressFormat.PNG if you use Bitmap.CompressFormat.JPEG your problem will repeat
public class SDCard {
public void setBitmap(Bitmap bitmap, String filename)
throws FileNotFoundException {
File folder = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/JANU");
if (!folder.exists()) {
folder.mkdir();
}
File imagefile = new File(folder, filename);
FileOutputStream fout = new FileOutputStream(imagefile);
boolean bit = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
}

Android set background from filepath

I would like to do something similar to imageView.setImageResource(R.drawable.myimage); but instead of providing an image from my app's resources, I would like to point to a file (/sdcard/.../image.jpg).
Is there a way to do that that does not involve loading the Bitmap and then setting the bitmap to the imageview ?
Thanks
You can update background from SDCard as follows...
String pathName = Environment.getExternalStorageDirectory().getPath() + "/folder/" + "image.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
imageView.setImageBitmap(bitmap);
Try this way
public Bitmap ImgBitFromFile(String file_name) {
File imgFile = new File(file_name);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
return myBitmap;
}
return null;
}
And used like:
img.setImageBitmap(ImgBitFromFile(File_Path));
And do not Forget to add READ Permission in your manifest.xml file.

Download and show the Thumbnail

I try to download a picture from URL to SD card/Download.
And I try to show its thumbnail in imageview.
Now I had below code:
try {
Download(URL); //download picture to SD card/Download
File myfile = new File(Environment.getExternalStorageDirectory() + "/Download/", filename);
Drawable photo = null;
photo = Drawable.createFromPath(myfile.getPath());
imageview.setBackgroundDrawable(photo);
}
It show the original picture.
But when the picture is large.
The memory error occurs.
So I want to show the smaller picture.
How should I do to generate the thumbnail and show it?
Or how to use the thumbnail generate by Android system?
Use Bitmap, Something like,
try
{
Download(URL); //download picture to SD card/Download
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory() + "/Download/", filename);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
imageview.setImageBitmap(imageBitmap);
}
catch(Exception ex) {
}
From the Shown Code
Try this instead your last 2 lines
Bitmap photo = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(myfile.getPath()),60,60,true);
imageview.setImageBitmap(photo);
And if you have made any objects for Bitmap/String/Stream in your Download() function free them calling System.gc();
And I hope this will work.

Categories

Resources