Skip to content

Commit

Permalink
Fix camera file path issue with Android api 24+
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson committed Feb 24, 2017
1 parent 1989448 commit bdce34d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@
<action android:name="com.xfrocks.api.androiddemo.ChatOrNotif" />
</intent-filter>
</receiver>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.file_provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

</manifest>
8 changes: 0 additions & 8 deletions app/src/main/java/com/xfrocks/api/androiddemo/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.xfrocks.api.androiddemo;

import android.content.Context;
import android.net.Uri;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

Expand All @@ -17,8 +15,6 @@
import net.gotev.uploadservice.Logger;
import net.gotev.uploadservice.UploadService;

import java.io.File;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
Expand Down Expand Up @@ -78,10 +74,6 @@ public synchronized static App getInstance() {
return sInstance;
}

public static Uri getTempForCamera(Context context) {
return Uri.fromFile(new File(context.getExternalCacheDir(), "camera.jpg"));
}

public static boolean getFeatureConfirmSignInWithRemember() {
return BuildConfig.FEATURE_CONFIRM_SIGN_IN_WITH_REMEMBER > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import com.xfrocks.api.androiddemo.App;
import com.xfrocks.api.androiddemo.BuildConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -38,7 +40,9 @@ public static Intent[] buildCameraIntents(Context context) {
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, App.getTempForCamera(context));
intent.putExtra(MediaStore.EXTRA_OUTPUT, getFileProviderUri(context, getCameraFile(context)));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

cameraIntents.add(intent);
}

Expand All @@ -51,7 +55,7 @@ public static Uri getUriFromChooser(Context context, Intent data) {
uri = data.getData();
}
if (uri == null) {
uri = App.getTempForCamera(context);
uri = Uri.fromFile(getCameraFile(context));
}

return uri;
Expand Down Expand Up @@ -86,4 +90,14 @@ public static String getFileNameFromUri(Context context, Uri uri) {

return "file.bin";
}

private static File getCameraFile(Context context) {
return new File(context.getExternalCacheDir(), "camera.jpg");
}

private static Uri getFileProviderUri(Context context, File file) {
// https://developer.android.com/training/camera/photobasics.html
String authority = BuildConfig.APPLICATION_ID + ".file_provider";
return FileProvider.getUriForFile(context, authority, file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private void onImageResized(Uri uri, Bitmap resource) {
suffix = "jpg";
}

File outputDir = getContext().getCacheDir();
File outputDir = getContext().getExternalCacheDir();
File outputFile;
try {
outputFile = File.createTempFile(prefix, "." + suffix, outputDir);
Expand All @@ -318,19 +318,19 @@ private void onImageResized(Uri uri, Bitmap resource) {
return;
}

uploadAttach(uri, outputFile.getPath(), fileName);
uploadAttach(outputFile.getPath(), fileName);
}

void uploadAttach(Uri uri) {
int size = App.getFeatureAttachmentResize();
if (size > 0) {
attemptResize(uri, size);
} else {
uploadAttach(uri, uri.getPath(), null);
uploadAttach(uri.getPath(), null);
}
}

private void uploadAttach(Uri uri, String path, String fileName) {
private void uploadAttach(String path, String fileName) {
ApiAccessToken accessToken = null;
if (mListener != null) {
accessToken = mListener.getEffectiveAccessToken();
Expand Down Expand Up @@ -395,7 +395,7 @@ public void onCancelled(Context context, UploadInfo uploadInfo) {
.startUpload();

Attachment attachment = new Attachment();
attachment.uri = uri;
attachment.uri = Uri.fromFile(new File(path));
attachment.uploadId = uploadId;
mPendingAttachments.addAttachmentAndNotify(attachment);
} catch (MalformedURLException | FileNotFoundException e) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>

0 comments on commit bdce34d

Please sign in to comment.