3

I need to open a specific image in Android Gallery, but I tried some codes with no success. This is my code:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse( "file:///Download/screenshot.jpg"), "image/*");
startActivity(intent);

It seems the Android Gallery is open but with no image, it show a black screen.

I'm trying my app in a Samsung Galaxy Nexus with ICS 4.0

Hope anyone can help me.

Thanks, Víctor.

lokoko
  • 5,719
  • 5
  • 33
  • 68
vicxros
  • 31
  • 1
  • 2
  • How does the file get there? it could be a permission problem, when the file is written it depends wether the MODE_WORLD_READABLE flag was set during writing. – Ian Warwick May 04 '12 at 16:04
  • There is a complete answer on [this](http://stackoverflow.com/questions/2169649/open-an-image-in-androids-built-in-gallery-app-programmatically) page. – se_bastiaan May 04 '12 at 16:10
  • Is the file path correct? Shouldn't there be sdcard in there somewhere? – FabianCook May 04 '12 at 16:20
  • 1
    Uri.parse( "file://sdcard/Download/screenshot.jpg") ?? – FabianCook May 04 '12 at 16:20
  • First at all, thank you every one you have answer my question. And special thanks to SmartLemon, because I only added "scard" to my path file and... It worked! – vicxros May 06 '12 at 19:01

1 Answers1

1

Usew Button And EditID To "btnLoadPicture" And ImageView ID "imgView" Use THe Following And Try Again

private static int RESULT_LOAD_IMAGE = 1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btnLoadPicture = (Button) findViewById(R.id.buttonLoadPicture);

    btnLoadPicture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        // String picturePath contains the path of selected Image
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}
Hossein Kurd
  • 2,426
  • 2
  • 39
  • 64
  • The answer is the question comments.. You dont seem to be loading `Intent.ACTION_VIEW` – IAmGroot Feb 24 '14 at 15:33
  • What To Explain? It's Too Simple – Hossein Kurd Feb 24 '14 at 15:36
  • Call Button "btnLoadPicture" , "i" Is new Intent set ACTION_PICK and .MediaStore.Images.Media.EXTERNAL_CONTENT_URI Once user will select an image, the method onActivityResult() of our main activity will be called – Hossein Kurd Feb 24 '14 at 15:50
  • when an image is selected from Image Gallery startActivityForResult() method Will handle the result back. – Hossein Kurd Feb 24 '14 at 15:51
  • onActivityResult gets called once an Image is selected, if the activity that was triggered was indeed Image Gallery, RESULT_LOAD_IMAGE Used for startActivityForResult() method we passed previously – Hossein Kurd Feb 24 '14 at 15:53