I have read this link :Open an image in Android's built-in Gallery app programmatically Get/pick an image from Android's built-in Gallery app programmatically, and the code looks well.
It results with following image: http://i.stack.imgur.com/vz3S8.png, but this is not the result I want.
I want to open the gallery similar to: http://i.stack.imgur.com/ZoUvU.png.
I want to choose the pic form the folder gallery.
Do you know how to modify the code?
I used:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.gallery", "com.android.camera.GalleryPicker"));
// intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Log.i("aa","adafdsfa");
startActivityForResult(intent, 1);
Through I get the folder gallery, but I cannot get the pic path.
File dir = new File(Environment.getExternalStorageDirectory().toString() + "/sdcard/yourfolder");
Log.d("File path ", dir.getPath());
String dirPath=dir.getAbsolutePath();
if(dir.exists() && dir.isDirectory()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
// tells your intent to get the contents
// opens the URI for your image directory on your sdcard
//its upto you what data you want image or video.
intent.setType("image/*");
// intent.setType("video/*");
intent.setData(Uri.fromFile(dir));
// intent.setType("media/*");
// intent.
startActivityForResult(intent, 1);
}
else
{
showToast("No file exist to show");
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (data==null) {
showToast("No image selected");
//finish();
}
else
{
Uri selectedImageUri = data.getData();
// String filemanagerstring = selectedImageUri.getPath();
//MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
if(selectedImagePath!=null)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(selectedImageUri);
startActivity(intent);
}
else
{
showToast("Image path not correct");
}
}
}
}
Related
This code is redirecting to drive with open navigation but not opening actual given path
OLD Code
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/foldername/");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(uri, "text/csv");
startActivity(intent);
The path value I am getting through uri is:
/storage/emulated/0/myfolder
NEW Code
Intent resultIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
resultIntent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri =
Uri.parse(Environment.getExternalStorageDirectory().getPath() +
"/myfolder/");
resultIntent.setType("*/*");
resultIntent.putExtra("android.provider.extra.INITIAL_URI", uri);
// show the entire internal storage tree
//resultIntent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivity(resultIntent);
Let me know the issue where is the problem in this code.
private static final int READ_REQUEST_CODE = 10;
...
/**
* Fires an intent to spin up the "file chooser" UI and select an image.
*/
public void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(uri, "text/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
After the user selects a document in the picker, onActivityResult()
gets called. The resultData parameter contains the URI that points to
the selected document. Extract the URI using getData(). When you have
it, you can use it to retrieve the document the user wants.
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
Log.i(TAG, "Uri: " + uri.toString());
showImage(uri);
}
}
}
I'm creating a program that can select image from gallery and after selected, the text next to the image will change to "Delete" for delete the image.
I have worked on select image from gallery, and now I don't know how to make the delete function. Can someone please help!
And here is my code. Where I should put the delete.
addPhotoText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectNewsFeedImage();
}
});
.....else if (items[item].equals("Choose from Gallery")) {
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Select File"), RESULT_LOAD_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RESULT_LOAD_IMAGE && data != null) {
Bitmap bm = null;
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
addPhotoIcon.setImageBitmap(bm);......
You need to get the path of the image that you have selected from the gallery first, next you need to delete that image from the sdcard using its path.
To get the path of the selected image refer this
Get filepath and filename of selected gallery image in Android
To delete the image using the path from sdcard
Android Delete Image from SD Card with OnClick
you should have to use URI here it is in the OnActivityResult
Uri selectedI = data.getData();
File file = new File(String.valueOf(selectedI));
file.delete();
I'm trying to open an image using intent.
The path of the image in my storage is /storage/emulated/0/nitp/download/logo.png.
My code is
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DEFAULT);
intent.setDataandType(Uri.parse("/storage/emulated/0/nitp/download/logo.png"),"image/*");
startActivity(intent);
I also tried putting file://storage/emulated/0/nitp/download/logo.png
and content://storage/emulated/0/nitp/download/logo.png
What is the path I should use?
Solved
Have to use file:///storage/emulated/0/nitp/downloads/logo.png
For Pick image from media Storage or SD Card:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
And For get the path of Image and set in ImageView:
#Override
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
imgpath= MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageViewProfileImage.setImageBitmap(imgpath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hope this work.
My application filmed a video and then back to a screen that allows me to choose another video or gallery display videos.
The problem is that the gallery does not display all the videos I record, just one. I'm sure I record the videos because they appear in My Files.
The code used to display the gallery is as follows:
final static int REQUEST_VIDEO_CAPTURED = 1;
private Uri uriVideo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video_list);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("video/*");
startActivityForResult(intent, 1);
//I also try this
// Intent intent = new Intent(Intent.ACTION_PICK,
// android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// intent.setType("video/*");
// startActivityForResult(intent, 1);
// and this
// Intent intent = new Intent();
// intent.setType("video/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
// startActivityForResult(Intent.createChooser(intent, "Select Video"),
// 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_VIDEO_CAPTURED) {
uriVideo = data.getData();
Toast.makeText(VideoList.this, uriVideo.getPath(), Toast.LENGTH_LONG).show();
ContentResolver res = getApplicationContext().getContentResolver();
Cursor cursor = MediaStore.Video.query(res, data.getData(), new String[] { MediaStore.Video.VideoColumns.DURATION });
if (cursor.moveToFirst()) {
duration = cursor.getString(0);
}
if (uriVideo != null) {
}
}
} else if (resultCode == RESULT_CANCELED) {
uriVideo = null;
finish();
}
}
I hope you understand, it's my first question.
Thank you very much!
Sorry for my bad English
you must request the system to update the media gallery
hi i am not able to getting call to method onActivityResult after below code.
private void ImageChooseOptionDialog() {
Log.i(TAG, "Inside ImageChooseOptionDialog");
String[] photooptionarray = new String[] { "Take a photo",
"Choose Existing Photo" };
Builder alertDialog = new AlertDialog.Builder(TabSample.tabcontext)
.setItems(photooptionarray,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Camera");
_path = Environment
.getExternalStorageDirectory()
+ File.separator
+ "TakenFromCamera.png";
Log.d(TAG, "----- path ----- " + _path);
media = _path;
// File file = new File(_path);
// Uri outputFileUri = Uri.fromFile(file);
// Intent intent = new Intent(
// android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT,
// outputFileUri);
// startActivityForResult(intent, 1212);
} else if (which == 1) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Gallary");
// Intent intent = new Intent();
// intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
// startActivityForResult(Intent
// .createChooser(intent,
// "Select Picture"), 1);
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent
.createChooser(intent,
"Select Picture"), 1);
Log.i(TAG, "end" + which);
}
}
});
alertDialog.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.create().show();
}
and this is my onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Inside Onactivity Result");
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Log.i(TAG, "Inside if else Onactivity Result");
// currImageURI is the global variable I’m using to hold the
// content:// URI of the image
Uri currImageURI = data.getData();
String path = getRealPathFromURI(currImageURI);
Log.i(TAG, "Inside Onactivity Result Image path" + path);
}
}
}
please let me knew where i am doing wrong. i am calling onActivityResult method from which=1 after dialog box appears.
but not getting any log inside onActivityResult method in logcat.
This is because, you may not be getting RESULT_OK. Always first check for request code and inside that check for result code. If you are retrieving image from gallery, try following code inside onActivityResult():
if (requestCode == TAKE_PICTURE_GALLERY) {
if (resultCode == RESULT_OK) {
final Uri selectedImage = data.getData();
final String[] filePathColumn = { MediaStore.Images.Media.DATA };
final Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
final int columnIndex = cursor
.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();
}
}
And use the filePath wherever you want.
I hope this solves your problem. Thank you :)
UPDATE:
Use this code to start your gallery activity:
imagePathURI = Uri.fromFile(new File(<your image path>));
final Intent intent = new Intent(Intent.ACTION_PICK, imagePathURI);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePathURI);
startActivityForResult(intent, TAKE_PICTURE_GALLERY);
When you want to retrieve image from gallery, the MediaStore.EXTRA_OUTPUT refers to The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video. So here, you have to pass the image path where u'll receive your image.
imagePathURI = Uri.fromFile(new File(<your image path>)); // here a file will be created from your image path. And when you'll receive an image, u can access the image from this image path.