Android photo sharing with FileProvider - java

I have read all the answer about this argument but I receive always an error of the application that receive my photo.
The only way that worked for me, for all application, was this (It works because sd card files are public to all applications):
final File tmpFile = new File(context.getExternalCacheDir(), "exported.jpg");
Uri tmpFileUri = Uri.fromFile(tmpFile);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setDataAndType(tmpFileUri, "image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, tmpFileUri);
context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_image)));
Now, I'm stuck on how to share a file that is located in a private folder.
I used the code provided by the google documentation:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.test.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
...
...
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="internal_files" path="/"/>
<cache-path name="internal_cache" path="/" />
</paths>
This is the code to share files using the FileProvider but doesn't work with any application except whats up:
final File tmpFile = new File(context.getCacheDir(), "exported.jpg");
Uri tmpFileUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", tmpFile);
//Remove the uri permission because we overwrite the file
context.revokeUriPermission(tmpFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
saveBitmapToPath(bitmap, tmpFile);
bitmap.recycle();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setDataAndType(tmpFileUri, "image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, tmpFileUri);
//Grant again the permissions
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_image)));
Why do I keep getting errors in other applications, like this:
java.lang.SecurityException: Permission Denial: content://com.test.myapp.fileprovider/internal_cache/exported.jpg (pid=675, uid=10052) requires null
Or
IllegalArgumentException: Failed to find configuration root that contains content://com.test.myapp.fileprovider/internal_cache/exported.jpg

Finally looking at the source code of the receiving app, I got the solution.
This is the complete, working code that I share.
I hope to help somebody:
<!-- AndroidManifest.xml -->
<provider
android:name="com.test.myapp.fileprovider.FileProvider"
android:authorities="com.test.myapp.fileprovider"
android:exported="true"
tools:ignore="ExportedContentProvider" />
//EntryPoint
private void mySharer() {
ArrayList<Uri> streamUris = new ArrayList<Uri>();
for (int i = 0; i < 10; i++) {
File tmpFile = new File(getContext().getCacheDir(), "tmp" + i + ".jpg");
Uri tmp = FileProvider.getUriForFile("com.test.myapp.fileprovider", tmpFile);
streamUris.add(tmp);
}
}
//Share Intent creator
public final void shareUris(ArrayList<Uri> streamUris) {
if (!streamUris.isEmpty()) {
Intent shareIntent = new Intent();
shareIntent.putExtra(ShareCompat.EXTRA_CALLING_PACKAGE, getPackageName());
shareIntent.putExtra(ShareCompat.EXTRA_CALLING_ACTIVITY, getComponentName());
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/jpeg");
if (streamUris.size() == 1) {
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, streamUris.get(0));
} else {
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, streamUris);
}
//For multiple images copy all images in the baseDir and use startActivityForResult
startActivityForResult(Intent.createChooser(shareIntent, getString(R.string.share_image)), 500);
}
}
//onResult you can delete all temp images/files with specified extensions
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 500:
getContentResolver().delete(FileProvider.getUriForFile(getPackageName() + ".fileprovider", null), FileProvider.WHERE_EXTENSION, new String[]{"jpg"});
break;
default:
break;
}
}
/**
* This class extends the ContentProvider
*/
abstract class AbstractFileProvider extends ContentProvider {
private final static String OPENABLE_PROJECTION_DATA = "_data";
private final static String[] OPENABLE_PROJECTION = { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE, OPENABLE_PROJECTION_DATA };
#Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if (projection == null) {
projection = OPENABLE_PROJECTION;
}
final MatrixCursor cursor = new MatrixCursor(projection, 1);
MatrixCursor.RowBuilder b = cursor.newRow();
for (String col : projection) {
if (OpenableColumns.DISPLAY_NAME.equals(col)) {
b.add(getFileName(uri));
} else if (OpenableColumns.SIZE.equals(col)) {
b.add(getDataLength(uri));
} else if (OPENABLE_PROJECTION_DATA.equals(col)) {
b.add(getFileName(uri));
} else {
b.add(null);
}
}
return cursor;
}
#Override
public String getType(Uri uri) {
return URLConnection.guessContentTypeFromName(uri.toString());
}
protected String getFileName(Uri uri) {
return uri.getLastPathSegment();
}
protected long getDataLength(Uri uri) {
return AssetFileDescriptor.UNKNOWN_LENGTH;
}
#Override
public Uri insert(Uri uri, ContentValues initialValues) {
throw new RuntimeException("Operation not supported");
}
#Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
throw new RuntimeException("Operation not supported");
}
#Override
public int delete(Uri uri, String where, String[] whereArgs) {
throw new RuntimeException("Operation not supported");
}
}
/**
* This class extends the AbstractFileProvider
*/
public class FileProvider extends AbstractFileProvider {
public static final String CONTENT_URI = "content://";
private File baseDir;
#Override
public boolean onCreate() {
baseDir = getContext().getCacheDir();
if (baseDir != null && baseDir.exists()) {
return true;
}
Log.e("FileProvider", "Can't access cache directory");
return false;
}
#Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
File f = new File(baseDir, uri.getPath());
if (f.exists()) {
return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
}
throw new FileNotFoundException(uri.getPath());
}
#Override
protected long getDataLength(Uri uri) {
File f = new File(baseDir, uri.getPath());
return f.length();
}
public static Uri getUriForFile(String authority, File file) {
return Uri.parse(CONTENT_URI + authority + "/" + file.getName());
}
}
-------------EDIT: 05/11/16--------------
Added support for multiple images:
Copy all images in the baseDir folder
Implement delete() method in the FileProvider
Use startActivityForResult
Listen onActivityResult
Now you can delete all temp images
For email attachment you must wait for email to be sent before delete the file, otherwise you'll send an empty attachment

Related

How to open file via intent right after downloading?

I need to open downloaded file by any relevant activity outside my app.
I download file via DownloadManager to default folder Environment.DIRECTORY_DOWNLOADS
I registered BroadcastReceiver in my activity, which shall to open file right after download via intent:
#Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
if (downloadManager != null) {
Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Intent openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.setDataAndType(Uri.parse(uriString), getMimeType(uriString));
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(openIntent, "Выберите приложение"));
}
}
}
}
}
But right after downloading I got an exception:
java.lang.RuntimeException: Error receiving broadcast Intent
caused by android.os.FileUriExposedException: file:///storage/emulated/0/Download/sample-20.pdf exposed beyond app through ClipData.Item.getUri()
What's wrong?
Correct way to open downloaded file with FileProvider
#Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
if (downloadManager != null) {
Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Intent openIntent = new Intent(Intent.ACTION_VIEW);
File file = new File(URI.create(uriString));
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
openIntent.setDataAndType(uri, getMimeType(uriString));
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(openIntent, "Выберите приложение"));
}
}
}
}
}
String getMimeType(String path) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}

How to use Intent.ACTION_OPEN_DOCUMENT in Android Pie

I'm making a profile photo change function using Retrofit in android pie.
So I succeeded in uploading the photos taken with the camera to the server.
But I don't know how to transfer the photos selected from the gallery to my server.
(I'm fine with any kind of code in Java Kotlin.)
I will upload the video later.
I searched a lot on Google, but it was hard to get the information I wanted.
Well done in the google doc but i don't know how to do it.
https://developer.android.com/guide/topics/providers/document-provider
The Google doc shows examples of using bitmap or inputstream or something.
Do I need a bitmap or inputstream to upload photos using Retrofit?
I actually need a valid uri.
public void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, PICTURES_DIR_ACCESS_REQUEST_CODE);
}
#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);
}
}
}
public void Edit_Profile (String Image_Uri) {
File file = new File(Image_Uri);
RequestBody requestBody = RequestBody.create(file, MediaType.parse("image/*"));
MultipartBody.Part body = MultipartBody.Part.createFormData("uploaded_file", Num+ID+".jpg", requestBody);
}
In fact, onActivityResult returns the following type of uri.
content://com.android.providers.media.documents/document/image%3A191474
So when I try to send it to my server using that uri I get a FileNotFoundException error.
This is a privacy restriction introduced in Android-Q. Direct access to shared/external storage devices is deprecated when an app targets API 29 and the path returned from getExternalStorageDirectory method is no longer directly accessible to apps. Use the app-specific directory to write & read files.
By default, apps targeting Android 10 and higher are given scoped access into external storage, or scoped storage. Such apps can see the following types of files within an external storage device without needing to request any storage-related user permissions:
Files in the app-specific directory, accessed using getExternalFilesDir().
Photos, videos, and audio clips that the app created from the media store.
Files in the app-specific directory, accessed using getExternalFilesDir().
Photos, videos, and audio clips that the app created from the media store.
Go through the documentation Open files using storage access framework
Coming to the context, One thing that you can do is that as
CommonsWare suggested use InputStreamRequestBody. Otherwise, copy
the selected file to your app sandbox folder IE, app-specific
directory, then access the file from there without any permission.
Just see the below implementation that works in Android-Q and later.
Perform file search
private void performFileSearch(String messageTitle) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intent.setType("application/*");
String[] mimeTypes = new String[]{"application/x-binary,application/octet-stream"};
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, messageTitle), OPEN_DIRECTORY_REQUEST_CODE);
} else {
Log.d("Unable to resolve Intent.ACTION_OPEN_DOCUMENT {}");
}
}
onActivityResult returned
#Override
public void onActivityResult(int requestCode, int resultCode, final Intent resultData) {
// The ACTION_OPEN_DOCUMENT intent was sent with the request code OPEN_DIRECTORY_REQUEST_CODE.
// If the request code seen here doesn't match, it's the response to some other intent,
// and the below code shouldn't run at all.
if (requestCode == OPEN_DIRECTORY_REQUEST_CODE) {
if (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()"
if (resultData != null && resultData.getData() != null) {
new CopyFileToAppDirTask().execute(resultData.getData());
} else {
Log.d("File uri not found {}");
}
} else {
Log.d("User cancelled file browsing {}");
}
}
}
File writing to app specific path
public static final String FILE_BROWSER_CACHE_DIR = "CertCache";
#SuppressLint("StaticFieldLeak")
private class CopyFileToAppDirTask extends AsyncTask<Uri, Void, String> {
private ProgressDialog mProgressDialog;
private CopyFileToAppDirTask() {
mProgressDialog = new ProgressDialog(YourActivity.this);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.setMessage("Please Wait..");
mProgressDialog.show();
}
protected String doInBackground(Uri... uris) {
try {
return writeFileContent(uris[0]);
} catch (IOException e) {
Log.d("Failed to copy file {}" + e.getMessage());
return null;
}
}
protected void onPostExecute(String cachedFilePath) {
mProgressDialog.dismiss();
if (cachedFilePath != null) {
Log.d("Cached file path {}" + cachedFilePath);
} else {
Log.d("Writing failed {}");
}
}
}
private String writeFileContent(final Uri uri) throws IOException {
InputStream selectedFileInputStream =
getContentResolver().openInputStream(uri);
if (selectedFileInputStream != null) {
final File certCacheDir = new File(getExternalFilesDir(null), FILE_BROWSER_CACHE_DIR);
boolean isCertCacheDirExists = certCacheDir.exists();
if (!isCertCacheDirExists) {
isCertCacheDirExists = certCacheDir.mkdirs();
}
if (isCertCacheDirExists) {
String filePath = certCacheDir.getAbsolutePath() + "/" + getFileDisplayName(uri);
OutputStream selectedFileOutPutStream = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
int length;
while ((length = selectedFileInputStream.read(buffer)) > 0) {
selectedFileOutPutStream.write(buffer, 0, length);
}
selectedFileOutPutStream.flush();
selectedFileOutPutStream.close();
return filePath;
}
selectedFileInputStream.close();
}
return null;
}
// Returns file display name.
#Nullable
private String getFileDisplayName(final Uri uri) {
String displayName = null;
try (Cursor cursor = getContentResolver()
.query(uri, null, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(
cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
Log.i("Display Name {}" + displayName);
}
}
return displayName;
}
Here's a perhaps more general solution that allows the AsyncTask class to be separate, rather than embedded in an activity. It also returns a response to your activity when the task is complete, and uses a ProgressBar rather than the deprecated ProgressDialog.
The async task:
public class FileLoader extends AsyncTask<Uri, Void, String>
{
private WeakReference<Context> contextRef;
public AsyncResponse delegate = null;
public interface AsyncResponse {
void fileLoadFinish(String result);
}
FileLoader(Context ctx , AsyncResponse delegate) {
contextRef = new WeakReference<>(ctx);
this.delegate = delegate;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(Uri... uris) {
Context context = contextRef.get();
ContentResolver contentResolver = context.getContentResolver();
Uri uri = uris[0];
try {
String mimeType = contentResolver.getType(uri);
Cursor returnCursor =
contentResolver.query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String fileName = returnCursor.getString(nameIndex);
InputStream inputStream = contentResolver.openInputStream(uri);
File downloadDir =
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File f = new File(downloadDir + "/" + fileName);
FileOutputStream out = new FileOutputStream(f);
IOUtils.copyStream(inputStream,out);
returnCursor.close();
return f.getPath();
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
delegate.fileLoadFinish(result);
super.onPostExecute(result);
}
}
and in your activity:
private static final int DIR_ACCESS_REQUEST_CODE = 13;
public void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/*");
String[] mimeTypes = new String[]{"application/gpx+xml","application/vnd.google-earth.kmz"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, "Choose KMZ or GPX file"), DIR_ACCESS_REQUEST_CODE);
} else {
Log.d("****File","Unable to resolve Intent.ACTION_OPEN_DOCUMENT");
}
}
#Override
public void onActivityResult(int requestCode, int resultCode,Intent resultData)
{
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == DIR_ACCESS_REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
if (resultData != null)
{
Uri uri = resultData.getData();
mProgressBar.setVisibility(View.VISIBLE);
new FileLoader(this,
new FileLoader.AsyncResponse(){
#Override
public void fileLoadFinish(String result){
processFile(new File(result));
mProgressBar.setVisibility(View.GONE);
}
}).execute(uri);
}
}
}
My example attempts to find either .kmz files or .gpx files. The progress bar (if you need one, for long-running file operations) needs to be initialised (and hidden) in OnCreate():
mProgressBar = findViewById(R.id.progressbar);
mProgressBar.setVisibility(View.GONE);
My 'processFile()' method takes a while manipulating a map in the main activity so I have waited until it's done before hiding the ProgressBar.
I remain surprised that it takes such a lot of code to perform such a simple operation: to copy a file and make it available for use!

Cannot show in ImageView a picture exported from the gallery

I am creating an Android application that needs to store some multimedia files in the internal storage. The user can choose that multimedia files from the picker. Those files must be available even when the user removes them, so they are copied to the internal storage.
Here's my code:
final Bitmap bitm = MediaStore.Images.Media.getBitmap( this.getContentResolver(), uri );
final int bitmapRawLength = bitm.getAllocationByteCount();
final ByteBuffer byteBuffer = ByteBuffer.allocate( bitmapRawLength );
bitm.copyPixelsToBuffer( byteBuffer );
data = byteBuffer.array();
final ByteArrayInputStream in = new ByteArrayInputStream( data );
db.store( in );
So, the bytes composing the image are copied into an average file inside the internal store through an InputStream. Apparently it works, since the file has contents.
Later the image is loaded in an ImageView:
private void loadImage(File imgFile)
{
if ( imgFile.exists() ) {
final Bitmap bitmap = BitmapFactory.decodeFile( mediaFile.getPath() );
this.ivPictureBox.setImageBitmap( bitmap );
} else {
this.abortDueToMissingFile( imgFile );
}
return;
}
Unfortunately, this does not work. When it is time to load that image, the ImageView goes blank, nothing is shown.
Actually, in the log appears the following message:
D/skia: --- Failed to create image decoder with message 'unimplemented'
If I use the file explorer in Android Studio and export the image to my computer, then GwenView fails with the message "Failed to load metadata".
How can I correctly store the image, with the complete information, or show it correctly, whatever is easier or feasible?
I have develop and test some code in this case. I hope it helps you.
Defining request codes:
private static final int REQUEST_CODE_KITKAT_PICK_PHOTO = 11;
private static final int REQUEST_CODE_PICK_PHOTO = 12;
To call image picker:
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose a photo"), REQUEST_CODE_PICK_PHOTO);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_KITKAT_PICK_PHOTO);
}
To receive the picked image and copy it, in your Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE_PICK_PHOTO) {
if (data == null || data.getData() == null) {
Toast.makeText(getApplicationContext(), "Error in retrieving photo!", Toast.LENGTH_SHORT).show();
return;
}
Uri uri = data.getData();
String destPath = getFilesDir() + File.separator + "image.jpg"; // an example path
File imageFile = null;
try {
imageFile = copy(uri, destPath);
} catch (IOException e) {
e.printStackTrace();
}
if (imageFile != null) {
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ivPictureBox.setImageBitmap(bitmap);
}
} else if (requestCode == REQUEST_CODE_KITKAT_PICK_PHOTO) {
if (data == null || data.getData() == null) {
Toast.makeText(getApplicationContext(), "Error in retrieving photo!", Toast.LENGTH_SHORT).show();
return;
}
Uri originalUri = data.getData();
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
}
String destPath = getFilesDir() + File.separator + "image.jpg"; // an example path
File imageFile = null;
try {
imageFile = copy(originalUri, destPath);
} catch (IOException e) {
e.printStackTrace();
}
if (imageFile != null) {
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ivPictureBox.setImageBitmap(bitmap);
}
}
}
}
// To copy the file:
private File copy(Uri inputUri, String destPath) throws IOException {
File inputFile = new File(ImageUtils.getPathFromUri(getApplicationContext(), inputUri));
File outputFile = new File(destPath);
if (!outputFile.exists()) {
outputFile.createNewFile();
}
FileInputStream inStream = new FileInputStream(inputFile);
FileOutputStream outStream = new FileOutputStream(outputFile);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
return outputFile;
}
ImageUtils.java:
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
public class ImageUtils {
#SuppressLint("NewApi")
public static String getPathFromUri(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* #param uri The Uri to check.
* #return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* #param uri The Uri to check.
* #return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* #param uri The Uri to check.
* #return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* #param uri The Uri to check.
* #return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
Here is the result:
Silly of me!
It is as simple as:
final InputStream in = this.getContentResolver().openInputStream( uri );
...and copy it whichever you want.
This works for URI's with both SCHEME_CONTENT and SCHEME_FILE schemes.

Android: How to avoid downloading a file again which is currently downloading

public class RemoteApkInstaller {
public void install(String url) {
listenForDownloadCompleteEvent();
downloadApk(url);
}
private void listenForDownloadCompleteEvent() {
IntentFilter downloadCompleteIntentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
downloadCompleteReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (isInitiatedApkDownload(intent)) {
if (isDownloadSuccessful()) {
handleDownloadComplete();
} else {
Toast.makeText(context, R.string.download_error, Toast.LENGTH_SHORT).show();
logFailure();
}
context.unregisterReceiver(downloadCompleteReceiver);
}
}
};
context.registerReceiver(downloadCompleteReceiver, downloadCompleteIntentFilter);
}
private void downloadApk(String url) {
Uri uri = Uri.parse(url);
apkFileName = uri.getLastPathSegment();
if (isFileExistsInDownloadDir()) {
handleDownloadComplete();
return;
}
DownloadManager.Request request = getDownloadRequest(uri);
downloadID = getDownloadManager().enqueue(request);
}
private boolean isFileExistsInDownloadDir() {
File directory = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File file = new File(directory, apkFileName);
return file.exists();
}
private void handleDownloadComplete() {
doOperationsOnDownloadedPackage()
installApk(context);
}
private void doOperationsOnDownloadedPackage() {
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
...
}
private void installApk(Context context) {
File directory = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File file = new File(directory, apkFileName);
String mimeType = downloadID == UNSET_DOWNLOAD_ID ? MIME_TYPE_FOR_APK_FILE : getDownloadManager().getMimeTypeForDownloadedFile(downloadID);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
Uri apkUri = FileProvider.getUriForFile(context, "my.package.fileProvider", file);
installIntent.setDataAndType(apkUri, mimeType);
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(installIntent);
} else {
Uri apkUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
Basically when I click a weblink, an instance of this class is created and the install() method invoked. The problem is when I click the weblink again while the previous download is still only partially complete. I'm trying to check the downloads folder to determine if file already exists and if so, proceed with next set of action.
Even if the previous download is partially complete, the flag is true so in this case I'm executing the doOperationsOnDownloadedPackage() method on an apk file which is not 100% downloaded and I see crashes obviously.
What I'm looking for is a way to avoid initiating a repeat download if the same file is currently downloading.
Any help appreciated.
Thanks

Android ENOENT with some files

I use a Intent to get pictures path and open them into a File. I can open files allocated in "Camera" folder like "/storage/emulated/0/DCIM/Camera/IMG_20160817_232858.jpg", but I cannot open files in locations like "/storage/emulated/0/Pictures/1634894_.png". Using file.exists() it just says that it doesn't.
Need to say that I'm using API 23 and I request READ_EXTERNAL_STORAGE, so this souldn`t be a problem... But I can't access those files even with that.
What can be wrong?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMG && 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);
File file = new File(picturePath);
if(file.exists()) {
} else {
}
cursor.close();
}
}
Updated: it doesn't happens with all the files from the same folder, just some of them, but they really exist since I can open them from the gallery.
Update2:
I use this Intent.
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
Update3:
Running time permissions:
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
SomeWork();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
}
else {
SomeWork();
}
Permissions in Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
The MediaStore will index images that are not accessible to you from the filesystem. There is no requirement for a DATA column to return a value that you can use.
Instead, stop trying to get a File, and use the Uri itself:
getContentResolver().openInputStream() to get an InputStream on it
DocumentFile.fromSingleUri() to get a DocumentFile for easy access to metadata (e.g., MIME type)
try{
// this works!!!
File csvfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/brcOrdersaved.csv");
final String filename = csvfile.toString();
if (!csvfile.exists()) {
// displayMsg(context, "No Saved Order: ");
return (false);
}
FileReader fr = new FileReader(filename);
BufferedReader reader = new BufferedReader(fr);
String csvLine = "";
final char Separator = ',';
final char Delimiter = '"';
final char LF = '\n';
final char CR = '\r';
boolean quote_open = false;
if (reader.equals(null)) {
displayMsg(context, "NULL");
return (false);//rww 11/13/2021
}
int i = 0;
while (!myendofcsvfile) {
csvLine = reader.readLine();
if (csvLine == null) {
myendofcsvfile = true;
}
// do stuff here
}
fr.close();
fileexists = true;
} catch (Exception e) {
String msg = "Can not Load Saved Order ";
fileexists = false;
return (fileexists);
}

Categories

Resources