I want to choose images from the Gallery. Please check the following code.
public class Camera extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
WebView localview;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void ChoosePhoto(WebView webview)
{
localview=webview;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
try {
FileInputStream fileis=new FileInputStream(selectedImagePath);
BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
byte[] bMapArray= new byte[bufferedstream.available()];
bufferedstream.read(bMapArray);
localview.loadUrl("javascript:ReceivePhoto(\""+bMapArray+"\")");
if (fileis != null)
{
fileis.close();
}
if (bufferedstream != null)
{
bufferedstream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
And included the activity in Manifest file. But after choosing the image, OnActivityResult is not called.
Can anyone please help me???
This is what i do for picking the images from the Gallery:
Activity launch:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, Comun.GALLERY_PIC_REQUEST);
Capture activity result:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
mUriImagen = data.getData();
// Do something
}
}
EDIT: Innecesary code removed.
Let's try this.
Your Camera class:
public class Camera {
private Activity mParentActivity;
private OnPhotoChosenListener mPhotoChosenListener;
// Declare an Iterface for comunicating with Activity
interface OnPhotoChosenListener{
public void onPhotoChosen();
}
public Camera(Activity parentActivity) {
mParentActivity = parentActivity;
}
public void ChoosePhoto(WebView webview)
{
mPhotoChosenListener.onPhotoChosen();
}
Your Activity:
public class MyActivity extends Activity implements OnPhotoChosenListener{
Camera myCamera;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myCamera = new Camera(this);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
try {
FileInputStream fileis=new FileInputStream(selectedImagePath);
BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
byte[] bMapArray= new byte[bufferedstream.available()];
bufferedstream.read(bMapArray);
localview.loadUrl("javascript:ReceivePhoto(\""+bMapArray+"\")");
if (fileis != null)
{
fileis.close();
}
if (bufferedstream != null)
{
bufferedstream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
#Override
public void onPhotoChosen(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, Comun.GALLERY_PIC_REQUEST);
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
The only thing that rest is how to call
ChooseFoto(Webview webview)
method. This is the part i don't undestand well how to do it.
Maybe you found the solution.
EDIT: Code added for implementing an Interface.
Related
I am using this code for getting image from gallery or taking picture.
Taking picture works perfectly.
Picture taken from camera will show on imageview,
But unlike image taken from gallery, its always blank.
public void fromCamera(int id) {
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, IMAGE_CAPTURE);
}
public void fromGallery(int id) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Pick a Picture"),
IMAGE_PICK);
}
public String getRealPathFromURI(Uri contentUri) {
try {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return contentUri.getPath();
}
}
private void imageFromCamera(int resultCode, Intent data) {
this.mButtonCarPhoto.setImageBitmap((Bitmap) data.getExtras().get("data"));
Uri selectedImageUri = data.getData();
mSelectedImagePath = getRealPathFromURI(selectedImageUri);
Log.e("Camera",mSelectedImagePath);
// mBase64Image = CONVERT_IMG_BASE64(mSelectedImagePath);
}
private void imageFromGallery(int resultCode, Intent data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mSelectedImagePath = cursor.getString(columnIndex);
Log.e("Gallery",mSelectedImagePath);
cursor.close();
this.mButtonCarPhoto.setImageBitmap(BitmapFactory.decodeFile(mSelectedImagePath));
//mBase64Image = CONVERT_IMG_BASE64(mSelectedImagePath);*/
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case IMAGE_PICK:
this.imageFromGallery(resultCode, data);
break;
case IMAGE_CAPTURE:
this.imageFromCamera(resultCode, data);
break;
default:
break;
}
}
}
What am I doing wrong?
why this ?
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Pick a Picture"), IMAGE_PICK);
if you just wanna choose from gallery, remove theze lines from your code.
Try this, it works perfectly for me:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
and then override onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1) { // from gallery
if (data != null) {
Uri photoUri = data.getData();
if (photoUri != null) {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
path = cursor.getString(columnIndex); // path is a static variable
cursor.close();
new GetImages().execute();
}
}
}
}
and this is the inner class GetImages, that updates the image view in a Async Task:
public class GetImages extends AsyncTask<Void, Void, JSONObject>
{
#Override
protected JSONObject doInBackground(Void... params)
{
Bitmap bitmap = BitmapFactory.decodeFile(path.toString().trim());
if (bitmap != null) {
bitmap = Bitmap.createScaledBitmap(bitmap, 500, 500, true);
currentBitmap = bitmap; // current Bitmap is a static variable
drawable = new BitmapDrawable(bitmap); // drawable is a static variable
// salvataggio foto in locale
db.open();
String bitmapString = BitMapToString(bitmap);
db.updateImage(bitmapString, userName);
db.close();
JSONObject json = savePhotoInRemote(bitmapString);
return json;
}
return null;
}
protected void onPostExecute(JSONObject result)
{
imageview_photo.setBackground(drawable); // QUI AGGIORNI LA TUA IMAGE VIEW
}
}
I have a problem that is i take a photo i want to save it in internal storage. but i don't know how do make it.Who can suggest some solution ? thanks.
There is my code:
public class ThreeFragment extends Fragment {
private static final String TAG = ThreeFragment.class.getSimpleName();
private static int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 1;
View root;
GridView gridView;
String mCurrentPhotoPath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_three, container, false);
dolist();
return root;
}
public void dolist() {
gridView = (GridView) root.findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getActivity()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
openGallery();
break;
case 1:
takePhoto();
break;
case 2:
Log.d(TAG, "333333333333333333333");
break;
default:
break;
}
}
});
}
private void takePhoto() {
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
private String saveToInternalSorage(Bitmap bitmap) {
ContextWrapper cw = new ContextWrapper(getActivity());
File dic = cw.getDir("TEST", Context.MODE_PRIVATE);
File mypath = new File(dic, ".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return dic.getAbsolutePath();
}
public void openGallery() {
Intent i = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE) {
if (resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
}
}
else if (requestCode == REQUEST_IMAGE_CAPTURE) {
if (resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
saveToInternalSorage(bitmap);
// setPath(saveToInternalSorage(bitmap));
}
}
}
}
*Try This :
File mImageFile is path where you want to store you camera image file.
Uri tempURI = Uri.fromFile(mImageFile);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
activity.startActivityForResult(i, CHOOSE_CAMERA_RESULT);
then in your onActivityResult
#Override
public void onActivityResultRAW(int requestCode, int resultCode, Intent data)
{
super.onActivityResultRAW(requestCode, resultCode, data);
switch(requestCode)
{
case CHOOSE_CAMERA_RESULT:
{
if (resultCode == RESULT_OK)
{
// here you image has been save to the path mImageFile.
Log.d("ImagePath", "Image saved to path : " + mImageFile.getAbsoultePath());
}
}
break;
}
}*
Try This:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
cameraIntent.putExtra("return-data", true);
mActPanelFragment.startActivityForResult(cameraIntent, ActivityConstantUtils.CAMERA_REQUEST);
// method
public Uri setImageUri() {
File file = new File(Environment.getExternalStorageDirectory().getPath(), ActivityConstantUtils.STORED_IMAGE_PATH+"/Images/WOMCamera");
if (!file.exists()) {
file.mkdirs();
}
ActivityConstantUtils.mCurrentPhotoPath = (file.getAbsolutePath() + "/"+"IMG_"+ System.currentTimeMillis() + ".jpg");
Uri imgUri = Uri.fromFile(new File(ActivityConstantUtils.mCurrentPhotoPath));
if (file.canWrite()) {
}
return imgUri;
}
After that write code for result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ActivityConstantUtils.GALLERY_INTENT_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
String imagePath = getFilePath(data);
}
}
private String getFilePath(Intent data) {
String imagePath;
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imagePath = cursor.getString(columnIndex);
cursor.close();
return imagePath;
}
I am a beginner of android development. I am trying to develop my own gallery. But I am facing a problem. When I go to SD card on my memory and choose a photo then I am trying to open image using my Image Viewer app. But when I select Image Viewer then another intent is called for choosing my image. But I directly want show the selected image on Imageview. Please anyone help me.
My code:
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.result);
pickImage();
}
public void pickImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// recyle unused bitmaps
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Log.v("roni", filePath);
cursor.close();
if(bitmap != null && !bitmap.isRecycled())
{
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(filePath);
imageView.setBackgroundResource(0);
imageView.setImageBitmap(bitmap);
// imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String getPath(Uri uri)
{
String[] projection={MediaStore.Images.Media.DATA};
Cursor cursor=managedQuery(uri,projection,null,null,null);
int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
/////After changing the code
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.result);
Intent i = new Intent(
Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// recyle unused bitmaps
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Log.v("roni", filePath);
cursor.close();
if(bitmap != null && !bitmap.isRecycled())
{
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(filePath);
//imageView.setBackgroundResource(0);
imageView.setImageBitmap(bitmap);
// imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String getPath(Uri uri)
{
String[] projection={MediaStore.Images.Media.DATA};
Cursor cursor=managedQuery(uri,projection,null,null,null);
int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index); } }
Let's assume you have an activity called ViewActivity that shows selected Image.
in your AndroidManifest.xml set your activity as a Image Viewer Activity by adding this :
<activity android:name=".ViewActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
in onCreate() method of ViewActivity catch passed Image by doing this :
Intent intent = getIntent();
Uri data = intent.getData();
//Check If data type is Image
if (intent.getType().indexOf("image/") != -1)
{
myImageView.setImageURI(data);
}
try this Code replace whole class plz if success then i will explain to you
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
In the following code when I click on Button it will link to Gallery and pick image/video from it, but I want, Button to be redirect to filemanger and pick txt file from it. Please help to do so.
public class MainActivity extends Activity {
Button b1;
private static final int SELECT_PHOTO = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
//photoPickerIntent.setType("Document/*");
//int SELECT_PHOTO;
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
// String[] filePathColumn = {MediaStore.Images.Media.DATA};
String[] filePathColumn = {Environment.getExternalStorageDirectory().getAbsolutePath()};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Intent i =new Intent(getApplication(), Activity2.class);
i.putExtra("path",filePath );
startActivity(i);
Log.d("Here", filePath);
cursor.close();
// Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Use this code to intent.
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image."),
GALLERY_INTENT_CALLED);
}
else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/jpeg");
startActivityForResult(intent,
GALLERY_KITKAT_INTENT_CALLED);
}
In your onActivityResult ;
public String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
if (requestCode == GALLERY_INTENT_CALLED
&& resultCode == Activity.RESULT_OK) {
originalUri = data.getData();
selectedImagePath = getPath(originalUri);
cropImageView.setImageBitmap(BitmapFactory.decodeFile(selectedImagePath));
System.out.println("GALLERY_INTENT_CALLED : "
+ originalUri.getPath());
} else if (requestCode == GALLERY_KITKAT_INTENT_CALLED
&& resultCode == Activity.RESULT_OK) {
originalUri = data.getData();
ParcelFileDescriptor parcelFileDescriptor;
try {
parcelFileDescriptor = getActivity().getContentResolver().openFileDescriptor(originalUri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
cropImageView.setImageBitmap(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try to pick only one particular image from the SD card.I am able to pick images from gallery and Photos app in kikat.But not getting file path when i pick image from recents am getting file path as null.
I tried https://stackoverflow.com/a/2636538/1140321.
This works for Kitkat
public class BrowsePictureActivity extends Activity{
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browsepicture);
imageView = (ImageView)findViewById(R.id.imageView1);
((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
if (Build.VERSION.SDK_INT < 19) {
selectedImagePath = getPath(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
imageView.setImageBitmap(bitmap);
}
else {
ParcelFileDescriptor parcelFileDescriptor;
try {
parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImageUri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
imageView.setImageBitmap(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
}
You need to add permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />