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));
Related
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");
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);
hello very good noon to all.Actually i want to select an image from gallery and then i want to save the selected image in Sdcard which is available in android device.
pick image from Sd card:
Intent mediaChooser = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
// comma-separated MIME types
mediaChooser.setType("image/*");
startActivityForResult(mediaChooser, RESULT_LOAD_WATER_IMAGE);
And On Activity Result :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
String path;
path = getRealPathFromURI(data.getData());
}
break;
}
Implementation Of getRealPathFromURI:
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();
}
}
Save Image To Sd Card On Click:
// TODO Auto-generated method stub
String root = Environment.getExternalStorageDirectory()
.toString();
File myDir = new File(root + "/Your Folder Name");
myDir.mkdirs();
String fname = "Your File Name";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try
{
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("error" + e);
}
Using the code below, you can pick up an image from gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
After that, the picked up image will be return by the onActivityResult() method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Get the image file path
final String imageFilePath = cursor.getString(0);
cursor.close();
//save it the sdcard
saveToSDCard(imageFilePath);
}
super.onActivityResult(requestCode, resultCode, data);
}
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);
I am starting a request for an image pick:
Intent intent = new Intent();
intent.setType( "image/*" );
intent.setAction( Intent.ACTION_GET_CONTENT );
startActivityForResult( Intent.createChooser( intent, "Choose"), PHOTO_GALLERY );
And getting the data back out in onActivityResult:
if( resultCode == Activity.RESULT_OK && requestCode == PHOTO_GALLERY )
{
U.log( data.getData() );
Bitmap bm = ... // built from the getData() Uri
this.postImagePreview.setImageBitmap( bm );
}
When I launch the Intent, I see some folders, such as sdcard, Drop Box, MyCameraApp, and so on.
If I chose a picture from sdcard, when I load the preview, it is the completely wrong image. The other folders don't seem to be giving me this problem.
Does anyone know why it'd let me pick one image, then give me the Uri for another?
EDIT: Here are some exampled logged getData()s:
Good:
content://com.google.android.gallery3d.provider/picasa/item/5668377679792530210
Bad:
content://media/external/images/media/28
EDIT: I'm still having issues, when picking from the sdcard folder of gallery.
Here is a bit more expansion of what I'm doing in onActivityResult:
// cursor
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = mContext.getContentResolver().query( selectedImage, filePathColumn, null, null, null );
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex( filePathColumn[0] );
String filePath = cursor.getString( columnIndex );
cursor.close();
// Cursor: /mnt/sdcard/Pic.jpg : /mnt/sdcard/Pic.jpg
U.log( "Cursor: " + filePath + " : " + Uri.parse( filePath ) );
// "regular"
// Regular: content://media/external/images/media/28 : content://media/external/images/media/28
U.log( "Regular: " + data.getDataString() + " : " + Uri.parse( data.getDataString() ) );
// Regular 2: content://media/external/images/media/28 : content://media/external/images/media/28
U.log( "Regular 2: " + data.getData() + " : " + data.getData() );
mPostImagePreview.setImageBitmap( BitmapFactory.decodeFile( filePath ) );
mPostImagePreview.setVisibility( View.VISIBLE );
They still set the wrong image. If I go into the Gallery, long press the image, and view its details I get:
TItle: Pic
Time: May 2, 2012
Width: 720
Height: 1280
Orientation: 0
File size: 757KB
Maker: Abso Camera
Model: Inspire 4G
Path: /mnt/sdcard/Pic.jpg
So, the Gallery is telling me the path is the same as the pick action, and the Gallery is rendering it correctly. So why on earth is it not rendering if I set it from onActivityResult?
Also, this is the code I'm using to fire the Intent now:
private void selectPhoto()
{
Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
intent.setType( "image/*" );
( ( Activity )mContext ).startActivityForResult( Intent.createChooser( intent, "Select Picture" ), PHOTO_GALLERY );
}
Sometimes the thumbnails in the gallery app can be outdated and show thumbnails for a different image. This can happen when the image ids are reused, for example when an image gets deleted and a new one is added using the same id.
Manage Apps > Gallery > Clear Data can fix this problem then.
This is the code to open gallery. However this the same what you have done. Also see the onActivityResult code which I used to retrive the selected image.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
PHOTO_GALLERY);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PHOTO_GALLERY:
if (resultCode == RESULT_OK) {
Uri selectedImageUri = Uri.parse(data.getDataString());
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
getApplicationContext().getContentResolver(),
selectedImageUri);
this.postImagePreview.setImageBitmap( bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
private static int RESULT_LOAD_IMAGE = 1;
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
OnActivity Result
#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));
}
}
try this one
//Put this code on some event
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE);
// When above event fire then its comes to this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK && requestCode==1){
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();
// Use it as per recruitment
actualBitmap =BitmapFactory.decodeFile(filePath);
}
}
Try this,
public class SelectPhotoActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath="";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, SELECT_PICTURE);
}
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);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
// here you can set the image
}
}
}
}