Kürzlich mache ich App Angebote mit Speichern von Bild und Laden von Bild auf externen Speicher. Ich bin ziemlich verwirrt mit Uri
, File
und StringPath
. Wenn Sie beispielsweise ein Bild aus der Galerie laden, wird Uri
verwendet.Uri vs Datei vs StringPath in Android
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
}
Dann, wenn dekodieren das Bild, verwendet es 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);
}
Dann, wenn Byte aus der Datei lesen, verwendet es 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();
}
Also, was ist der Unterschied? Ist es nur in der Verwendung anders, aber dasselbe Verzeichnis darstellen?
Vielen Dank für Ihre Erklärung schreiben. Kannst du mir helfen, einen Blick auf http://stackoverflow.com/questions/36466483/how-the-codes-work-for-loading-image-from-gallery-android – SamTew
@SamTew Ich beantwortete Ihre Frage dort – Pooya
I habe nicht gemerkt, dass die Person, die sie beantwortet, du bist. Vielen Dank – SamTew