i want to choose a picture from SD card of the mobile. i am using below code to choose and to display in my activity
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Uri uri = Uri.parse(selectedImagePath);
uploadimage.setImageURI(uri);
It is working fine, but I want to convert this image into Bitmap, I have image path and URI.
How to convert image to Bitmap in this case? Please help me, thanks in advance.
use this code
Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
Related
I know how to open gallery intent and to display in the imageview.
But I am facing problem in selecting image automatically in oncreate method and display in the imageview.
I have an image in gallery folder with name "myfile.jpg"
Can anyone guide me how to do it without openting gallery intent.
Thank you in advance
Here is solution :
ImageView image = (ImageView)findViewById(R.id.myImage);
File root = Environment.getExternalStorageDirectory();
String path = root.getAbsolutePath();
path = path + File.separator + "myfile.jpg"; //where you image file located
Bitmap myBitmap = BitmapFactory.decodeFile(path);
image.setImageBitmap(myBitmap);
This code gives me image taken from android camera ,however,its size and resolution format of the thumnail.
Bitmap photo = (Bitmap) data.getExtras().get("data");
How can I get the original image with the original size and resolution
Thank you
Bitmap photo = (Bitmap) data.getExtras().get("data");
this code gives you only thumnail image.To get the original image u need to use EXTERNAL_STORAGE which image saved there. (Code is something like this)
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
For more information take a look at here
Create a Temp File in externalCacheDir and put the URI in your Intent which starts the camera, after image shooting the image is stored in that file.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempCaptureFile = ImageAccessor.createTempFile(getActivity(), TEMP_CAPTURE_FILE_NAME);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempCaptureFile));
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
getActivity().startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
ImageAccessor is just a helper class to create a File in externalCacheDir.
Bitmap photo = (Bitmap) data.getExtras().get("data");
this line only return thumbnal image reffer bellow link
android camera intent
I am using AQuery to load images taken by camera and displaying them in my activity in a image view. The problem is that when I try to do the same but instead of taking a picture, just select the image from my gallery, my image view appears to have nothing in it. This is my code:
//Get image uri that was selected in gallery
Uri selectedImage = data.getData();
//Convert uri to string path
strMainPic = selectedImage.toString();
//Create file to add as parameter to AQuery
File Main = new File(strMainPic);
aq.id(R.id.image_one).image(Main, 100);
If I use the selectedImage and change it to a Bitmap with BitmapFactory, it works but the performance suffers. What am I doing wrong?
I just solved it a couple of seconds ago. I used this code:
Uri selectedImage = data.getData();
try {
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
aq.id(R.id.image_one).image(bmp, AQuery.RATIO_PRESERVE);
I just added this to my onActivityResult() method.
I am trying to get the image from sdcard to display it in an imageview in android. Its showing error as SkImageDecoder:factory returned null in android emulator2.2. Here is my code to retrieve image from SDcard to imageview in android.
File imgFile = new File("/sdcard/wm.png");
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(myBitmap);
}
Please resolve this issue.
Use Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory instead of hard coding the path of the sd card (i.e "/sdcard/wm.png").
I am trying to display a .jpg from my sd card into an imageview in my layout. I dont get any errors, but nothing gets displayed. I would appreciate any help. Thanks
EDIT:
layout1 = (LinearLayout) findViewById(R.id.layout1Activity);
String imgFile = Environment.getExternalStorageDirectory() + "/Apple.jpg";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
ImageView myImage = new ImageView(this);
myImage.setImageBitmap(myBitmap);
layout1.addView(myImage);
do NOT access the SD card directly, try accessing it trough Environment. Like this:
String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";
and then you can call BitmapFactory:
Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);