I am Trying to choose a PDF from android SD card like this
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose Documents"), DOCUMENT_SELECT_PDF);
This code returns me the URI of the selected PDF file something like this
URI Value
content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D420
Can you guys help, how I can get actual path from this URI?????????
Rest Of code is Here
case DOCUMENT_SELECT_PDF:
if (resultCode == RESULT_OK) {
try {
final Uri pdfUri = imageReturnedIntent.getData();
documentFilePath = DocumentHelper.getPDFPath(this, pdfUri);
if (documentFilePath != null) {
documentName = DocumentHelper.getFileName(documentFilePath);
documentMimeType = DocumentHelper.getMimeType(this, pdfUri);
uploadDocuments();
}
} catch (Exception e) {
e.printStackTrace();
}
}
break;
Related
I am trying to choose/capture an image from gallery/camera(respectively) using the following code:
val pickIntent = Intent()
pickIntent.type = "image/*"
pickIntent.action = Intent.ACTION_GET_CONTENT
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val pickTitle = "Select or take a new Picture"
val chooserIntent = Intent.createChooser(pickIntent, pickTitle)
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, arrayOf(takePhotoIntent)
)
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE)
However, I always get result code for activity as -1 when the onActivityResult function is invoked. I seem to have data when the gallery option is used by nothing when the camera is invoked.
I dont have any exception to return and don't know what to debug.
I am using an emulator to test my application.
This 'result code for activity as -1' means that RESULT_OK.
Use '.getData()' to retrieve the result, and decide what you want to do with it.
switch (requestCode) {
case REQUEST_IMAGE_CAPTURE: {
if (resultCode == RESULT_OK) {
try {
Uri uri = data.getData();
Log.d(TAG, uri.toString());
} catch (Exception e) { e.printStackTrace(); }
}
} break;
default: break;
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.setType("image/*");
intent.setType("video/*");
// intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, READ_REQUEST_CODE)
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
uri1 = uri;
Log.i("anderwidth", "Uri: " + uri.toString());
File file = new File(uri1.getPath());
}
}
I am using retrofit with android. thats what happening? I cannot get to read the images and videos as files, file.exists give me false.Everytime its content//...
it works if someone downloaded filemanager app, not if not downloaded.
If you want to 'read' from path then open an InputStream and read() from the stream.
InputStream is = getContentResolver().openInputStream(uri);
I'm trying to open my photo with intent to view it in my phone's default gallery app, but when I do so, my app flashes and reloads without providing any errors. I'm trying to open my photo like this:
Uri uri = FileProvider.getUriForFile(context, "www.markwen.space.fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpg");
startActivity(intent);
I cannot find anything wrong in here. I'm thinking if it would be when I save my photo, which is taken using the camera, I didn't encode it correctly. When I save my photo, I simply follow the instructions here: https://developer.android.com/training/camera/photobasics.html
So how do you guys save your photo after you take the photo with your phone's camera app? What can I do to open up my photo? Thanks
Update:
here is how I take the photo:
private void startPhotoIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create photo file
File photo = new File(getFileDirectory(".jpg"));
// Save intent result to the file
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getContext(), "www.markwen.space.fileprovider", photo));
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Start intent to take the picture
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
here is the way I am saving the photos I took:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Photo
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == -1) {
Uri imageUrl = data.getData();
if (imageUrl != null) {
// Announce picture to let other photo galleries to update
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(imageUrl);
getActivity().sendBroadcast(mediaScanIntent);
try {
MediaStore.Images.Media.insertImage(getContext().getContentResolver(), directory + filename, "Multimedia-Image", "Multimedia-Image");
Toast.makeText(getContext(), "Image saved to gallery", Toast.LENGTH_LONG).show();
filename = "";
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
Get the absolut path of your image
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(file.getAbsolutePath())));
i'm not sure what your trying to do,
saving the picture to the phone is easy,
deepending on what intent you are using.
i would look for a way to change the intent so to specify the saving location for the image itself
Google for EXTRA_STREAM EXTRA_OUTPUT
I am using this code it is working fine in 1.6 but it is crashing in 4.4?
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = new ExternalStorage(getApplicationContext()).getAlbumStorageDir(); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
/*in onActivityResult*/
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else if (resultCode == RESULT_CANCELED) {
} else {
}
}
I have gone through several post but all I didn't find correct one.
There is no requirement for ACTION_IMAGE_CAPTURE to return a Uri via onActivityResult().
You know where the image should be stored, as you are providing that via EXTRA_OUTPUT. Use the value you put into EXTRA_OUTPUT, rather than data.getData().
I am trying to share an image from within my app and the image is on the external storage of the device. The problem is that the user can still opt to share the image if they have manually deleted it from external storage. How do I check if they have deleted it first? Here is my share method:
#SuppressLint("NewApi")
private void shareImage(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//create new files
File f = new File(mExternalImagePath);
if (f.exists()) {
//Do action
f.setReadable(true, false);
//create new file in the system
try {
f.createNewFile();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
//create new file object from the absolute path
File f1 = f.getAbsoluteFile();
f1.setReadable(true, false);
Uri path = Uri.fromFile(f1);
intent.putExtra(Intent.EXTRA_STREAM, path );
Intent mailer = Intent.createChooser(intent, null);
//mailer.setType("image/*");
startActivity(mailer);
}else{
Log.d("not exist", "not exist");
}
}
It works but always shares so if the image was manually deleted, it will try sending a blank image.
I'm not sure whether your issue is because you're creating a new file by calling f.createNewFile() or something else at large.
Either way, you should be able to greatly simplify your code to just get the stream and send the intent like so:
private void shareImage(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//create new files
File f = new File(mExternalImagePath);
if (f.exists()) {
Uri path = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, path);
Intent mailer = Intent.createChooser(intent, null);
startActivity(mailer);
}else{
Log.d("not exist", "not exist");
}
}
This will hopefully either clear up your issue, or make the issue more apparent and help you on your way.
You can also add a check for the file length, to ensure that it exists in some sort of readable form before you send your intent by calling .length() on f.
if (f.length() == 0){
Log.d("File Empty", "File does not have any content");
}else{
// create the intent and send
}
As #FD_ said, the createNewFile() was the problem. Be sure to add the mailer.setType("image/*"), so other apps can handle your sharing request.
#SuppressLint("NewApi")
private void shareImage(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
File f = new File(mExternalImagePath);
if (f.exists()) {
Uri path = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, path );
Intent mailer = Intent.createChooser(intent, null);
mailer.setType("image/*");
startActivity(mailer);
}else{
Log.d("not exist", "not exist");
}
}