Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uri vs File vs StringPath in android

Tags:

android

Recently I am doing app deals with saving image and loading image on external storage. I am quite confused with Uri, File and StringPath.

For example, when load image from Gallery, it uses Uri.

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { //Browse Gallery is requested
    //Get the path for selected image in Gallery
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    //Access Gallery according to the path
    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();

    loadImage(picturePath);         //load picture according the path
    image_View.setImageBitmap(pic); //Show the selected picture
}

Then when decode the image, it uses StringPath.

private void loadImage(String picturePath) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(picturePath,options);
    int height_ = options.outHeight;
    int width_ = options.outWidth;
    float ratio = width_/height_;
    int width = 480;
    int height = 480;
    if(width_>height_){
        height = Math.round(width / ratio);
    }else{
        width = Math.round(width*ratio);
    }

    options.inSampleSize = calculateInSampleSize(options, width, height);
    options.inJustDecodeBounds = false;
    pic=BitmapFactory.decodeFile(picturePath,options);
}

Then when read byte from file, it uses File.

File cacheDir = getBaseContext().getCacheDir();
//Form a directory with a file named "pic"
File f = new File(cacheDir, "pic");

try {
    //Prepare output stream that write byte to the directory
    FileOutputStream out = new FileOutputStream(f);
    //Save the picture to the directory
    pic.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

So, what is the difference? Is it just different in usage but represent same directory?

like image 203
SamTew Avatar asked Apr 06 '16 15:04

SamTew


1 Answers

Content URI looks something like:

content://media/external/images/media/53

The role of ContentResolver here is to get you an access to the image based on this URI, you don't need to know the filename or other properties of the file, you just need this URI to access the image.

String Path is the physical address of the image that stored which looks like:

file:///mnt/sdcard/myimage.jpg

and finally, File is the lowest handler which you need to operate with files. It uses the String Path as argument to create or open the file for read/write.

In your provided example here is the progress:

1- You ask ContentResolver to give you the real file path based on the provided URI

2- you load a bitmap file to a pic object based on the provided Path

3- you create a file named "pic" and compress the pic object to JPG and write to it

like image 95
Pooya Avatar answered Oct 31 '22 02:10

Pooya