Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android reading an image from SD Card

I'm trying to read an image from my SD Card and I'm not sure if I'm doing it right or not. Need some help please

This is my code for reading it:

String imageInSD = "/sdcard/Hanud/" + c.getString(1) + ".PNG";
        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
        ImageView myImageView = (ImageView)findViewById(R.id.imageview);
        myImageView.setImageBitmap(bitmap);

And this is my main file:

<?xml version="1.0" encoding="utf-8"?>
<WebView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

 <!-- <ImageView
    android:id="@+id/image"
    android:scaleType="center"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    />-->

    <ImageView
  android:id="@+id/imageview"
  android:layout_gravity="center"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:scaleType="center"
  />


  <TextView  
    android:id="@+id/myNameText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
  />
</WebView>
like image 424
moe Avatar asked Apr 10 '11 16:04

moe


People also ask

How do I view photos from my SD card on my phone?

Most Android phones come preinstalled with at least one file manager app. A file manager app lets you view and work with the files sitting on both internal and SD card storage on your device. This app may be called File Manager, Files, or something similar and should be available in the app drawer of your device.

Can I view pictures saved on SD card?

You can use the SD card reader to copy photo or video files to a computer for viewing. You can also go to Album on the mobile App to download the photo or video files to your phone and view it in the App under “Album.”

How do I get all images from internal or external storage on Android 10?

In order to fetch the images which are created by my application, we have to make a query on ContentResolver with the INTERNAL_CONTENT_URI. But if you want to get all images that are created by any application then make a query on ContentResolver with EXTERNAL_CONTENT_URI.


2 Answers

You can use the following code:

File sdCard = Environment.getExternalStorageDirectory();

File directory = new File (sdCard.getAbsolutePath() + "/Pictures");

File file = new File(directory, "image_name.jpg"); //or any other format supported

FileInputStream streamIn = new FileInputStream(file);

Bitmap bitmap = BitmapFactory.decodeStream(streamIn); //This gets the image

streamIn.close();

You would need to run FileInputStream in a try/catch block.

like image 37
AliR Avatar answered Sep 25 '22 08:09

AliR


It should be:

String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Hanud/" + c.getString(1) + ".PNG";
        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
        ImageView myImageView = (ImageView)findViewById(R.id.imageview);
        myImageView.setImageBitmap(bitmap);

This will ensure you find the correct directory!

like image 83
Jack Satriano Avatar answered Sep 25 '22 08:09

Jack Satriano