Pass image picked from gallery to other intent - android

I want to pass image picked from gallery to other intent. But when I click upload button and then select image from gallery it shows me the same activity. As if i am stuck on that activity. Can anyone help?????
This is my code..
if (resultCode == RESULT_OK && data != null) {
Uri photouri = data.getData();
if (photouri != null) {
try {
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(photouri,
filePath, null, null, null);
cursor.moveToFirst();
int ColIndex = cursor.getColumnIndex(filePath[0]);
String FilePath = cursor.getString(ColIndex);
cursor.close();
photo = BitmapFactory.decodeFile(FilePath);
Intent intent2 = new Intent(Activity1.this, Activity2.class);
intent2.putExtra("BitmapImage", photo);
startActivity(intent2);
} catch (Exception e) {
}
}
Code in Activity2
pic1= (Bitmap) this.getIntent().getParcelableExtra("BitmapImage");
img.setImageBitmap(pic1);

I am doing this in my app but instead of calling decodeFile in first activity,
I just passed the actual filename (path) selected from the gallery and
let the second activity decode it.
Intent intent2 = new Intent(Activity1.this,Activity2.class);
Activity2.intent2.putString("BitmapFilename",FilePath);
startActivity(intent2);

Related

pass uri to another activity and convert it to image

How to send a uri path of an image to another activity and convert it to image. I tried the below
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
//file name
Uri selectedImage = data.getData();
Intent i = new Intent(this,
AddImage.class);
i.putExtra("imagePath", selectedImage);
startActivity(i);
and get it like this
String imagePath = getIntent().getStringExtra("imagePath");
imageview.setImageURI(Uri.parse(imagePath ));
Convert you URI to String while adding to Intent like given below
i.putExtra("imagePath", selectedImage.toString());
and in your NextActivity get the String and convert back to URI like ->
Intent intent = getIntent();
String image_path= intent.getStringExtra("imagePath");
Uri fileUri = Uri.parse(image_path)
imageview.setImageURI(fileUri)
First Activity
Uri uri = data.getData();
Intent intent=new Intent(Firstclass.class,secondclass.class);
intent.putExtra("imageUri", uri.toString());
startActivity(intent);
Second class
Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo);
Bundle extras = getIntent().getExtras();
myUri = Uri.parse(extras.getString("imageUri"));
iv_photo.setImageURI(myUri);
to use the returned uir from the calling activity and then set it to a imageview you can do this
Uri imgUri=Uri.parse(imagePath);
imageView.setImageURI(null);
imageView.setImageURI(imgUri);
This is a workaround for refreshing an ImageButton, which tries to cache the previous image Uri. Passing null effectively resets it.
For converting the inputStream into a bitmap you could do this
InputStream in = getContentResolver().openInputStream(Uri.parse(imagePath));
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(in));
and then call
image.setImageBitmap(bm);
to set it it a imageview,
you could also check this link for an example
hope i could help
in Next activity get that URI like this;
Intent intent = getIntent();
String image_path= intent.getStringExtra("YOUR Image_URI");
and to convert that Image_URI to Image use Below mentioned Code
File imgFile = new File(image_path);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
}
To pass an image Uri to the next activity, you can just use setData() and getData(). There is no need to convert the Uri to anything.
First Activity
Intent intent = new Intent(this, SecondActivity.class);
intent.setData(uri);
startActivity(intent);
Second Activity
// get Uri
Uri uri = getIntent().getData();
// decode bitmap from Uri
if (uri == null) return;
try {
InputStream stream = getContentResolver().openInputStream(uri);
if (stream == null) return;
Bitmap bitmap = BitmapFactory.decodeStream(stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

android passing image file path to A then to B and finally C acitivity

I'm new to android and to java trying to learn my way in.
Right now I am trying to achieve (A)Select image form gallery (B) show a preview and ( C ) activity is Uploading to server:
Select image form gallery and show a preview: done (achieved) by using below Code
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// call android default gallery
startActivityForResult(intent, PICK_FROM_GALLERY);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == PICK_FROM_GALLERY) {
if (resultCode == RESULT_OK) {
fileUri = data.getData();
filePath = getRealPathFromURI(getApplicationContext(), fileUri);
Intent imagePreview = new Intent(MainActivity.this, ImagePreview.class);
imagePreview.putExtra("filePath", filePath);
startActivity(imagePreview);
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}}
}
And In ImagePreview.java
Intent imagePreview = getIntent();
// image or video path that is captured in previous activity
filePath = imagePreview.getStringExtra("filePath");
displayImage(filePath);
private void displayImage(String filePath) {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapHelper.decodeSampledBitmap(filePath, 300, 250));
Now According to my understanding, since the filePath is already a string, I should be able it pass it to uploadactivity.java as such
private void upload(){
Intent upload = new Intent(ImagePreview.this, UploadActivity.class);
upload.putExtra("finalImage", filePath);
startActivity(upload);
}
and in uploadactivity.java
Intent upload = getIntent();
// image or video path that is captured in previous activity
finalImage = upload.getStringExtra("finalImage");
By this I can get to UpoloadActivity and upload button is displayed but filePath is not passed.
What am I doing wrong?
You're not using the same key for the extras:
upload.putExtra("finalImage", filePath);
upload.getStringExtra("filePath");
Replace
finalImage = upload.getStringExtra("filePath");
with
finalImage = upload.getStringExtra("finalImage");

Force certain app to receive the intent (not chooser) [duplicate]

I'm looking for a way to open the Android gallery application from an intent.
I do not want to return a picture, but rather just open the gallery to allow the user to use it as if they selected it from the launcher (View pictures/folders).
I have tried to do the following:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
However this is causing the application to close once I select a picture (I know this is because of the ACTION_GET_CONTENT), but I need to just open the gallery.
Any help would be great.
Thanks
This is what you need:
ACTION_VIEW
Change your code to:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I hope this will help you. This code works for me.
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
ResultCode is used for updating the selected image to Gallery View
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);
if (cursor == null || cursor.getCount() < 1) {
return; // no cursor or no record. DO YOUR ERROR HANDLING
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
if(columnIndex < 0) // no column index
return; // DO YOUR ERROR HANDLING
String picturePath = cursor.getString(columnIndex);
cursor.close(); // close cursor
photo = decodeFilePath(picturePath.toString());
List<Bitmap> bitmap = new ArrayList<Bitmap>();
bitmap.add(photo);
ImageAdapter imageAdapter = new ImageAdapter(
AddIncidentScreen.this, bitmap);
imageAdapter.notifyDataSetChanged();
newTagImage.setAdapter(imageAdapter);
}
You can open gallery using following intent:
public static final int RESULT_GALLERY = 0;
Intent galleryIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );
If you want to get result URI or do anything else use this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case QuestionEntryView.RESULT_GALLERY :
if (null != data) {
imageUri = data.getData();
//Do whatever that you desire here. or leave this blank
}
break;
default:
break;
}
}
Tested on Android 4.0+
Following can be used in Activity or Fragment.
private File mCurrentPhoto;
Add permissions
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
Add Intents to open "image-selector" and "photo-capture"
//A system-based view to select photos.
private void dispatchPhotoSelectionIntent() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
this.startActivityForResult(galleryIntent, REQUEST_IMAGE_SELECTOR);
}
//Open system camera application to capture a photo.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(App.Instance.getPackageManager()) != null) {
try {
createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (mCurrentPhoto != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhoto));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
Add handling when getting photo.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_IMAGE_SELECTOR:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = App.Instance.getContentResolver().query(data.getData(), filePathColumn, null, null, null);
if (cursor == null || cursor.getCount() < 1) {
mCurrentPhoto = null;
break;
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
if(columnIndex < 0) { // no column index
mCurrentPhoto = null;
break;
}
mCurrentPhoto = new File(cursor.getString(columnIndex));
cursor.close();
} else {
mCurrentPhoto = null;
}
break;
case REQUEST_IMAGE_CAPTURE:
if (resultCode != Activity.RESULT_OK) {
mCurrentPhoto = null;
}
break;
}
if (mCurrentPhoto != null) {
ImageView imageView = (ImageView) [parent].findViewById(R.id.loaded_iv);
Picasso.with(App.Instance).load(mCurrentPhoto).into(imageView);
}
super.onActivityResult(requestCode, resultCode, data);
}
As you don't want a result to return, try following simple code.
Intent i=new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivity(i);
if somebody still getting the error, even after adding the following code
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
then you might be opening the intent in another class (by calling) due to this the app will crash when you click the button.
SOLUTION FOR THE PROBLEM
Put intent in the class file which is connected to the button's layout file.
for example- activity_main.xml contains the button and it is connected to MainActivity.java file. but due to fragmentation, you are defining the intent in some other class(lets says Activity.java) then your app will crash unless you place the intent in MainActivity.java file. I was getting this error and it is resolved by this.

Picking an image from the new Photos application

I am using the following Intent to allow the user to choose a picture
Intent pictureIntent = new Intent();
pictureIntent.setType("image/*");
pictureIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(pictureIntent, GALLERY_PICK_IMAGE_REQUEST);
After getting the result, I am using the following approach (using this method)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_PICK_IMAGE_REQUEST) {
if (resultCode == mActivity.RESULT_OK) {
Uri selectedImage = data.getData();
Log.d(TAG, "Gallery image path = " + selectedImage.getPath());
launchUploadImageActivity(getRealPathFromURI(mActivity, selectedImage));
}
}
}
private String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
When I use the new Photos application to choose the picture, I can choose a photo that I presume is on google's servers, since it returns a url to a picture, instead of the file path:
Log output:
Gallery image path = /0/https://lh6.googleusercontent.com/lRkls4SQwi_afJvjO5QChsWqRwTpDjg-....
Is there any way I can force the user to choose pictures that are local on the phone?
Try with following code to chose picture
Intent pictureIntent = new Intent(MediaStore.ACTION_PICK, Images.Media.INTERNAL_CONTENT_URL);
startActivityForResult(pictureIntent, REQUEST_CHOOSE_IMAGE);
instead of
Intent pictureIntent = new Intent();
pictureIntent.setType("image/*");
pictureIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(pictureIntent, GALLERY_PICK_IMAGE_REQUEST);
Add this line to your intent:
pictureIntent.setData(Images.Media.INTERNAL_CONTENT_URI);

Start Activity not working properly in this scenario?

Hi every one in the below code after the image has been selected it is not moving to next activity ,in gallery if we select first item it remains in the same activity but we select another item other than first position image it is moving to next activity
startActivity(mv); the shown startactvity is not calling when we click on the first position image
but the toast is appearing as image has been selected but not moving to next activty
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
case R.id.imageView2:
upLoadPhoto();
break;
protected void upLoadPhoto() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
startActivityForResult(intent, 100);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && data != null && data.getData() != null) {
System.out.println("in case");
Uri _uri = data.getData();
if (_uri != null) {
// User had pick an image.
Cursor cursor = getContentResolver()
.query(_uri,
new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
null, null, null);
cursor.moveToFirst();
// Link to the image
final String imageFilePath = cursor.getString(0);
Log.v("imageFilePath", imageFilePath);
File photos = new File(imageFilePath);
try {
gbmp = BitmapFactory.decodeStream(
new FileInputStream(photos), null, null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cursor.close();
}
mv = new Intent(Imageselection.this, Modeselection.class);
mv.putExtra("test", gbmp);
mv.putExtra("name", 100);
System.out.println("going to gamestart class");
startActivity(mv);
Toast.makeText(getApplicationContext(), "Image selected", Toast.LENGTH_SHORT).show();
}
Its because you passed the whole image to the bundle.
The bundle has limited Size, you cannnot put the image itself into the intent.
You need to save your image to a cache, then either pass the image's file name or file path to the putExtra and then retrieve it later by accessing the filename or file path.
For your case, you select an image from gallery, then you can get the URI or path of that image, put the URI/path to the intent, and retrieve it on your another activity.
When you call an intent to launch the gallery, it will return with data which contains the selected file's Uri.
Here r some sample code you may need if you launch the default gallery:
// Launch Gallery to choose pic.
Intent intentLaunchGallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intentLaunchGallery, LOAD_IMAGE_ACTIVITY_REQUEST_CODE);
...
private String getPath(Uri uri) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
...
// Gallery launched to choose picture
if (requestCode == LOAD_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
fileUri = data.getData();
filePath = getPath(fileUri);
// fileUri = Uri.parse(filePath);
// call media scanner to refresh gallery
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{filePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.i("MediaScanner", "Scanned " + path + ":");
Log.i("MediaScanner", "-> uri=" + uri);
}
});
// Toast.makeText(this, "Image chosen from: " + filePath, Toast.LENGTH_LONG).show();
Log.d("MainMenu->onActivityResult", "Image chosen from: " + filePath);
// display the picture chosen by user
Intent intentShowMarkers = new Intent(MainMenuActivity.this, ShowMarkersActivity.class);
intentShowMarkers.putExtra("IMG", filePath);
intentShowMarkers.putExtra("FLAG", false);
MainMenuActivity.this.startActivity(intentShowMarkers);
} else if (resultCode == RESULT_CANCELED) {
// user pressed the cancel of gallery
Toast.makeText(MainMenuActivity.this, "Cancelled.", Toast.LENGTH_SHORT).show();
}
}
try below code. for select an image from gallary
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]);
filePath = cursor.getString(columnIndex);
cursor.close();
and in your onActivityResult method write below intent to go for next activity
Intent picIntent = new Intent(CurrentActivity.this,
NextActivity.class);
picIntent.putExtra("gallery", filePath);
startActivity(picIntent);
in your NextActivity class onCreate Method write below code
ImageView imageView = (ImageView)findViewById(R.id.imgView);
String fileString = getIntent().getStringExtra("gallery");
imageView.setImageBitmap(BitmapFactory.decodeFile(fileString));

Categories

Resources