how to display image file save on internal storage in android - android

i have saved drawable images in internal storage by compressing them in bitmap format. the problem is that i want to retrieve those image files and wants to display them in grid view i am able to list the total no of files but i am not able to display those files in image view. here is my code
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File[] imageList = directory.listFiles();
if(imageList == null){
imageList = new File[0];
}
Log.i("My","ImageList Size = "+imageList.length);
imageView.setImageResource(imageAdapter. (....?) );

set Image(From Sd card) to imageview get the path of the image which is in sdcard. and you can assign it by creating Bitmap
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "your_image_name.png";
Bitmap bmp = BitmapFactory.decodeFile(filePath);
imageView.setImageBitmap(bmp);
Another way to set imageview you can also use third party library also which may have resize and Scaling options too. like ImageLoader

Related

Displaying from external storage?

I have an image saved in my Pictures folder, how to display it in a imageview?
like:
imageview.setimage("//Pictures//cat.jpg)
I know it's not a correct code, but I want to achieve something like this, hope someone can help, thanks!
you first generate a bitmap from file path and then put that bitmap to imageview
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
Edit: Also add this permission in your manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You can set image like this from the sd card get path and create file variable and decode file using BitmapFactory set imageview image
String path = Environment.getExternalStorageState()+"/Pictures//cat.jpg";
File f = new File(path);
imageview.setImageBitmap(new BitmapFactory.decodeFile(f.getAbsolutePath()));
First you passing wrong file path.
for Correct File path do like this
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath());
then create your URL like.
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath()) + "/cat.jpg";
Then use like this.
File image = new File(filePath);
imageView.setImageBitmap(new BitmapFactory.decodeFile(image.getAbsolutePath()));
Got it working with combination of the 2 posted codes, thanks for everyone, here's the working code:
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath();
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES) + "/picFolder/1.jpg";
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
image1.setImageBitmap(bitmap);

Image display from sd card

I am new to Android. In my app, I want to access a particular image from my sd card. But the image is not displayed. I have include WRITE_EXTERNAL_STORAGE request in my manifest.
public class Display extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
final TextView name=(TextView) findViewById(R.id.name);
final TextView phone_no=(TextView) findViewById(R.id.phone_no);
File f= new File("/storage/sdcard0/Download/images.jpeg");
Bitmap bMap = BitmapFactory.decodeFile(f.getAbsolutePath());
imageView.setImageBitmap(bMap);
}
I also tried the following codes, but of no use
File mFichier = new File(Environment.getExternalStorageDirectory(),"/storage/sdcard0/Download/images.jpeg");
if(mFichier.exists())
{
imageView.setImageURI(Uri.fromFile(mFichier));
}
and also this code
Bitmap mBitmap = BitmapFactory.decodeFile("/storage/sdcard0/Download/images.jpeg");
imageView.setImageBitmap(mBitmap);
Please help me as to why my image is not getting displayed..
First of all, you want to load file from external storage like sdcard, you'd better use following code:
public File getDataFolder(Context context) {
File dataDir = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
dataDir = new File(Environment.getExternalStorageDirectory(), "myappdata");
if(!dataDir.isDirectory()) {
dataDir.mkdirs();
}
}
if(!dataDir.isDirectory()) {
dataDir = context.getFilesDir();
}
return dataDir;
}
It will return a folder which is named "myappdata" located in your sd-card. After that, if you want to load a image from that folder, you can use following code:
File cacheDir = getDataFolder(this);
File cacheFile = new File(cacheDir, "images.jpeg");
InputStream fileInputStream = new FileInputStream(cacheFile);
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = scale;
bitmapOptions.inJustDecodeBounds = false;
Bitmap wallpaperBitmap = BitmapFactory.decodeStream(fileInputStream, null, bitmapOptions);
ImageView imageView = (ImageView)this.findViewById(R.id.preview);
imageView.setImageBitmap(wallpaperBitmap);
If you still have problem with above code, you can check the full example here:
Android Save And Load Downloading File Locally

How display images using Imageloader on ImageView from sdcard path?

I have downloaded all images in sdcard from URL. Now I want to display images in GridView using ImageLoader but I am unable to do this.
My image sdcard path like : /mnt/sdcard/DPImages/image_product3.jpg
private String[] mFileStrings;
private File[] listFile;
public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"DPImages");//sdcard/DpImages
if (file.isDirectory())//if files is directory
{
listFile = file.listFiles();
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
mFileStrings[i] = listFile[i].getAbsolutePath();//mFileStrings with uri of images
}
}
}
You can use the mFileStrings to display the images in gridview.
In your getView of adapter.
ImageView image=(ImageView)convertView.findViewById(R.id.imageview);
Bitmap b = BitmapFactory.decodeFile(mFileStrings[position]);
image.setImageBitmap(b);
I'm using universal image downloader.
You can show the image with this:
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
imageLoader.displayImage(imageUri, imageView);
The link is this. https://github.com/nostra13/Android-Universal-Image-Loader

Android: Displaying .jpg image from sdcard using ImageView

I am trying to display a .jpg from my sd card into an imageview in my layout. I dont get any errors, but nothing gets displayed. I would appreciate any help. Thanks
EDIT:
layout1 = (LinearLayout) findViewById(R.id.layout1Activity);
String imgFile = Environment.getExternalStorageDirectory() + "/Apple.jpg";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
ImageView myImage = new ImageView(this);
myImage.setImageBitmap(myBitmap);
layout1.addView(myImage);
do NOT access the SD card directly, try accessing it trough Environment. Like this:
String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";
and then you can call BitmapFactory:
Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);

How to delete a bitmap image which is stored on the sd card in android

In my application I have a grid view of images and when a user clicks an image then it will open the image in fullscreen. The images are loaded from the SD card as follows:
File sdDir = new File("mnt/sdcard/Pictures");
File[] sdDirFiles = sdDir.listFiles();
for(File singleFile : sdDirFiles) {
String filePath = singleFile.getAbsolutePath();
Bitmap bmp = scaleBitmap(filePath);
photos.add(bmp);
}
mThumbIds = photos.toArray(new Bitmap[(photos.size())]);
}
Scale bitmap is a method which decodes each file into a bitmap and then scales the bitmap before returning it.
I then have another Activity which loads the images fullscreen once they have been clicked. I have a menu button "Delete", from which I would like to delete the file on the sdcard which represents the bitmap I see on the screen.
The problem I have is that there is no way to get the file name from the Bitmap object, so therefore I cannot delete the file.
Any help will be greatly appreciated.
You could extend the Bitmap class and add a filename field. Or, you could pass the filename to your new activity in the intent bundle.

Categories

Resources