Uygulamamda kullanmak için bir Uri'den bir Bitmap nesnesi nasıl alınır ( /data/data/MYFOLDER/myimage.pngveya içinde saklamayı başarırsam
file///data/data/MYFOLDER/myimage.png)?
Bunun nasıl yapılacağı hakkında bir fikri olan var mı?
Uygulamamda kullanmak için bir Uri'den bir Bitmap nesnesi nasıl alınır ( /data/data/MYFOLDER/myimage.pngveya içinde saklamayı başarırsam
file///data/data/MYFOLDER/myimage.png)?
Bunun nasıl yapılacağı hakkında bir fikri olan var mı?
Yanıtlar:
. . ÖNEMLİ: Daha iyi bir çözüm için aşağıdaki @Mark Ingram ve @pjv yanıtlarına bakın. . .
Bunu deneyebilirsiniz:
public Bitmap loadBitmap(String url)
{
Bitmap bm = null;
InputStream is = null;
BufferedInputStream bis = null;
try
{
URLConnection conn = new URL(url).openConnection();
conn.connect();
is = conn.getInputStream();
bis = new BufferedInputStream(is, 8192);
bm = BitmapFactory.decodeStream(bis);
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (bis != null)
{
try
{
bis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return bm;
}
Ancak unutmayın, bu yöntem yalnızca bir iş parçacığından (GUI -dread değil) çağrılmalıdır. Ben bir AsyncTask.
İşte bunu yapmanın doğru yolu:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
}
Çok büyük görüntüler yüklemeniz gerekiyorsa, aşağıdaki kod döşemeye yüklenir (büyük bellek ayırmalarından kaçınır):
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(myStream, false);
Bitmap region = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
Cevabı burada görün
Bellek kullanımını da takip ederek doğru şekilde yapmanın yolu:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = getThumbnail(imageUri);
}
}
public static Bitmap getThumbnail(Uri uri) throws FileNotFoundException, IOException{
InputStream input = this.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither=true;//optional
onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) {
return null;
}
int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inDither = true; //optional
bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//
input = this.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
return bitmap;
}
private static int getPowerOfTwoForSampleRatio(double ratio){
int k = Integer.highestOneBit((int)Math.floor(ratio));
if(k==0) return 1;
else return k;
}
Mark Ingram'ın gönderisinden getBitmap () çağrısı da decodeStream () öğesini çağırır, böylece herhangi bir işlevsellik kaybetmezsiniz.
Referanslar:
getPowerOfTwoForSampleRatio()atlanabilir. Bakınız: developer.android.com/reference/android/graphics/…
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(c.getContentResolver() , Uri.parse(paths));
}
catch (Exception e)
{
//handle exception
}
ve evet yolu böyle bir formatta olmalıdır
file:///mnt/sdcard/filename.jpg
Bu en kolay çözümdür:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
private void uriToBitmap(Uri selectedFileUri) {
try {
ParcelFileDescriptor parcelFileDescriptor =
getContentResolver().openFileDescriptor(selectedFileUri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
O görünüyor MediaStore.Images.Media.getBitmapkullanımdan kaldırıldı API 29. Tavsiye edilen yol ImageDecoder.createSourceeklendi API 28.
Bitmap'in nasıl alınacağı aşağıda açıklanmıştır:
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(requireContext().contentResolver, imageUri))
} else {
MediaStore.Images.Media.getBitmap(requireContext().contentResolver, imageUri)
}
Bitmap'i böyle uri'den alabilirsiniz
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (IOException e) {
e.printStackTrace();
}
Uri imgUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgUri);
Aşağıdaki gibi startActivityForResult metodunu kullanın
startActivityForResult(new Intent(Intent.ACTION_PICK).setType("image/*"), PICK_IMAGE);
Ve şöyle sonuç alabilirsiniz:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case PICK_IMAGE:
Uri imageUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
Bir çok yol denedim. Bu iş benim için mükemmel.
Galeri'den pictrue seçerseniz. Sen almanın eşya olması gerekiyor Uriden intent.clipdataveya intent.databunlardan biri farklı sürümde boş olabilir çünkü.
private fun onChoosePicture(data: Intent?):Bitmap {
data?.let {
var fileUri:Uri? = null
data.clipData?.let {clip->
if(clip.itemCount>0){
fileUri = clip.getItemAt(0).uri
}
}
it.data?.let {uri->
fileUri = uri
}
return MediaStore.Images.Media.getBitmap(this.contentResolver, fileUri )
}
bu yapıyı yapabilirsiniz:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
Bundle extras = imageReturnedIntent.getExtras();
bitmap = extras.getParcelable("data");
}
break;
}
Bu şekilde bir uri'yi bitmap'e kolayca dönüştürebilirsiniz. umut yardım et.
InputStream imageStream = null;
try {
imageStream = getContext().getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
(KOTLIN) 7 Nisan 2020 itibariyle, yukarıda belirtilen seçeneklerin hiçbiri işe yaramadı, ama benim için işe yarayan:
Bitmap'i bir val'de saklamak ve onunla bir imageView ayarlamak istiyorsanız, bunu kullanın:
val bitmap = BitmapFactory.decodeFile(currentPhotoPath).also { bitmap -> imageView.setImageBitmap(bitmap) }
Bitmap'i ve imageView olarak ayarlamak istiyorsanız, bunu kullanın:
BitmapFactory.decodeFile(currentPhotoPath).also { bitmap -> imageView.setImageBitmap(bitmap) }
Mobil galeriden görüntü uri almak için tam yöntem.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try { //Getting the Bitmap from Gallery
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
rbitmap = getResizedBitmap(bitmap, 250);//Setting the Bitmap to ImageView
serImage = getStringImage(rbitmap);
imageViewUserImage.setImageBitmap(rbitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}