Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image from sdcard in android?

i want create simple app in which i want get the image from sdcard but can`t success. my code is below

File myFile = new File("/sdcard/photo.jpg");
ImageView jpgView = (ImageView)findViewById(R.id.imageView);
BitmapDrawable d = new BitmapDrawable(getResources(), myFile.getAbsolutePath());
jpgView.setImageDrawable(d);

but can`t get the image.

like image 346
Manish Avatar asked Jul 31 '12 12:07

Manish


3 Answers

Use below code instead of your code.

File f = new File("/mnt/sdcard/photo.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);
like image 53
Dipak Keshariya Avatar answered Oct 21 '22 04:10

Dipak Keshariya


package com.example.facebook;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Facebookhome extends Activity {
    Button share;
    Uri screenshotUri;
    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facebookhome);
        share = (Button) findViewById(R.id.button1);
        img = (ImageView) findViewById(R.id.imageView1);

        share.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"),
                        SELECT_PICTURE);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}
like image 32
user2185718 Avatar answered Oct 21 '22 03:10

user2185718


try this...

Bitmap bitmap = BitmapFactory.decodeFile(myFile.getAbsolutePath());
jpgView.setImageBitmap(bitmap);
like image 42
MAC Avatar answered Oct 21 '22 04:10

MAC