Search code examples
androidfile-uploadcameraandroid-imageview

Getting path of captured image from imageviews and upload to server


I have four image views in my screen and capture images from camera and displayed in it, now i want to upload those images into the server. So i have used the below code to get the path of those but it's through the exception. Is there any possible to upload the images into the server from image view which is capture from camera(without store the images into the memory).

         if (requestCode == 1&& resultCode == RESULT_OK)
                {
                    Bitmap photo = (Bitmap) data.getExtras().get("data");
                    purchase_Img_1.setImageBitmap(photo);
                    Uri tempUri = getImageUri(getApplicationContext(), photo);
                    File finalFile = new File(getRealPathFromURI(tempUri));

                }

    public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

Solution

  • Getting path of captured image from imageviews 
    

    There is no path. There are no paths. You got bitmaps and placed those in views.

    Uri tempUri = getImageUri(getApplicationContext(), photo);
    

    Here you stored a bitmap using MediaStore. You got back an uri.

    You can open an inputstream for that uri, read from it and upload the bytes.

    But with using the MediaStore you saved the bitmap to file. If you do not want that you better compress the bitmap to a ByteArrayOutputStream and upload the resulting byte array.