Android share Intent share image doesn't exist - java

I'm trying to share an image file which saved to internal storage using share intent.
after I intent image file doesn't display.
I used following code to save the image file
try {
FileOutputStream fos = openFileOutput("twitterimage.jpg", Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
bikeBm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
}
//intent code
File internalFile = getFileStreamPath("twitterimage.jpg");
Uri uri = Uri.fromFile(internalFile);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,new File(uri));
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
when I check the file existance using above "uri", the image file exists
in the internal storage but checking the file using
String file = "data/data/com.bike.app/twitterimage.jpg";
File f = new File(file);
shows me the file not exist.
pls help me to solve this problem.

If i am gessing right you want to share images between to applications. Your probleme is that you used Context.MODE_PRIVATE when you write a file so other applications cannot have access to this file.
To share an image you can use MediaProvider to restrict access to you Media or simply used Context.MODE_WORLD_READABLE but the file will be public readable then.
hope that help

Related

How to Get parcelFileDescriptor from URI?

In my app I am receiving intent to open the pdf file using "application/pdf" intent-filter in menifest.
After a lot of research I am trying following code to open the file.
try {
File f = new File("file://"+uri.getPath());
Toast.makeText(k.this, f.getAbsolutePath(), Toast.LENGTH_LONG).show();
Toast.makeText(k.this, f.exists()?"Y":"N", Toast.LENGTH_LONG).show();
parcelFileDescriptor = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
} catch (IOException e) {
e.printStackTrace();
}
When I click on a file from file manager and open it with my app, It says the file does not exist. That means I am not able to create file from URI.
So my question is how to get parcelFileDescriptor from URI.
Thanks to the comment of Mike M.
getContentResolver().openFileDescriptor()
opens the fileDescriptor correctly.

Why is the received URI of the PDF files incorrect? [duplicate]

I am working on an app where I want to be able to export and import some data from the app, on a .txt file.
The minimum API of the app is 21.
The export part works well, but I am having trouble with the import part.
I open the file explorer :
butImportPatient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
startActivityForResult(intent, IMPORTPATIENT_ACTIVITY_REQUEST_CODE);
}
});
This looks like it is working.
But my onActivityResult doesn't work, I didn't find how I can get the file from the Uri.
For now, here is my code :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMPORTPATIENT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
File file = new File(data.getData().getPath()) ;
String path = file.getAbsolutePath() ;
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append("\n");
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
AlertDialog.Builder builder = new AlertDialog.Builder(this) ;
builder.setMessage(path)
.show() ;
}
}
It is a mix of multiple posts I saw here, but none seems to work.
I get this path :
/document/home:List.txt
It creates FileNotFoundException. How can I get the real path of the file ?
I didn't find how I can get the file from the Uri.
There is no file. ACTION_OPEN_DOCUMENT and ACTION_GET_CONTENT do not open a file. They open a document. That document might be a file. It might not.
That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
How can I get the real path of the file ?
You don't.
If you wish to only accept files, integrate a file chooser library instead of using ACTION_OPEN_DOCUMENT or
ACTION_GET_CONTENT. Just bear in mind that filesystem access to external storage is limited on Android 10+.
If you use ACTION_GET_CONTENT, and the scheme of the Uri that you get is file, then getPath() will be a filesystem path.
Otherwise, you need to understand that you have no idea where the document is coming from, and stop thinking in terms of "real path of the file". Use ContentResolver and openInputStream() to make a copy of the content to some file that you control, then work with that file.

Saving an ImageView to Gallery - Code isn't working

This is my first question here. I've searched for my doubt. I found similar questions but i haven't exactly got my answer. So please forgive me if i have done something wrong.
I'm trying to save an image from an ImageView in my app to a folder in my sdcard. Here's the code :-
public void save(View view) {
myImage.setDrawingCacheEnabled(true);
Bitmap imgV = myImage.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/AVP_saved");
String fname="Image.png";
File file = new File(myDir, fname);
try {
FileOutputStream out = new FileOutputStream(file);
imgV.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Toast.makeText(this, "Image Downloaded", 7000).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), 8000).show();
}
}
'save' method is the method assigned to a button. 'myImage' is the ImageView found by its id. I have already set the permissions in the manifest. The thing is, the image dosen't get saved and it says that the path dosen't exist. When I myself create the folder "AVP_saved", then the image gets saved. What do i have to edit in this code such that the app creates the folder by itself when i click the button?
Thanks for your time!
Add this code after File myDir = new File(root + "/AVP_saved");
if(!myDir.exists()) {
mydir.mkdir(); //you can else call mkdirs() if you have to create a complete directory hierarchy
}
It seems that in Java, it's not possible to create a directory hierarchy by creating just a file in it.
With this, you'll create your directory only if it doesn't exist (be careful, if the directory exists but it's a file, it will launch an exception maybe, so you can look for myDir.isDirectory() too).

Share a file without saving it on the external storage

I am using the following code to allow the user to share a bitmap,
try {
File save_dir = Environment.getExternalStorageDirectory();
FileOutputStream out = new FileOutputStream(save_dir
+ "/test.jpg");
final_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///" + save_dir + "/test.jpg"));
startActivity(Intent.createChooser(share,
getString(R.string.share_dialog_title)));
} catch (Exception e) {
Toast.makeText(mainActivity, "Error in sharing.",
Toast.LENGTH_LONG).show();
}
the problem is, the if the device has no SD Card, the file will not be saved.
is there is any good way to save it to the internal storage, in somewhere other apps can access, since saving it in the applications data directory will not allow other applications to access it.
Regards

How to read PDF file with url in Android

My Code is :
File file = new File(docpath);
String mimetype = "application/pdf";
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, mimetype);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If I want to use the application/pdf method
What should I do?
PDFs are not naturally supported in android. You will need to use an open source library.
Android PDF Viewer, for example.
http://code.google.com/p/apv/

Categories