user3304617
user3304617

Reputation: 113

How to retrieve image path stored in database and display in image view ?

I am doing a project in which user select the image and we have to store the path of the image. I am able to display the picture in imageView but when the application closes or user presses back button image is not displaying. So I have to hold the image forever until user deletes the image.

Following is my code:

Button buttonLoadImage = (Button) findViewById(R.id.pre1);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }


    @SuppressWarnings("unused")
    @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();

            Bitmap yourSelectedImage = BitmapFactory.decodeFile(picturePath);

            //testimage.setImageBitmap(yourSelectedImage);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            ImageView imageView = (ImageView) findViewById(R.id.im2);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            pd.open();
            pd.insert(picturePath);
            pd.close();
        }
    }

Upvotes: 0

Views: 3564

Answers (2)

Piyush
Piyush

Reputation: 18923

Generally when you want to save your image in your database then it has many ways to store it.

1) You can save your image as a imageName in database.

2) You can also save path of image into your database and when you retrieve that path from database in string after that you can decode that path generally we can do when we are fetching from gallery.

3) You can save your image as byte[] in your database and when you want to retrieve it then you can retrieve as a BLOB image and after that convert that byte[] to Bitmap and set that bitmap to your ImageView.

Upvotes: 1

Praveen Sharma
Praveen Sharma

Reputation: 4348

see you can do two things:-

  1. If you want image should be remain in its original place then you can use its path only,and save that path to some place like sharedPreference, fileSystem,sqlite DB, or any var.
  2. another way you can copy that image to your application file then you can use that image

if you have path of image then you can get image like as:-

private void loadImageFromStorage(String path)
    {

        try {

//  if you want to use the same path then use this
Bitmap b = BitmapFactory.decodeFile(pathName);
                ImageView img=(ImageView)findViewById(R.id.imgPicker);
            img.setImageBitmap(b);
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }

    }

don't forget to give permission <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Upvotes: 0

Related Questions