Skip to content

Commit

Permalink
Upload: use FileProvider for taking pictures, thx k.a.a.
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0rg committed Oct 7, 2019
1 parent 814592e commit a1dc9a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
9 changes: 9 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@
<provider android:name=".data.RosterProvider"
android:exported="false"
android:authorities="${applicationId}.provider.Roster" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider.Files"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>

<activity android:name="de.duenndns.ssl.MemorizingActivity" />

Expand Down
14 changes: 9 additions & 5 deletions src/org/yaxim/androidclient/chat/ChatWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.os.*;
import android.provider.MediaStore;

import org.yaxim.androidclient.BuildConfig;
import org.yaxim.androidclient.FileHttpUploadTask;

import org.yaxim.androidclient.MainWindow;
Expand All @@ -35,6 +36,7 @@

import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;

Expand Down Expand Up @@ -99,6 +101,7 @@ public class ChatWindow extends ThemedActivity implements OnKeyListener,
private static final int CHAT_MSG_LOADER = 0;
private int lastlog_size = 200;
private int lastlog_index = -1;
private File cameraPictureFile = null;
private Uri cameraPictureUri = null;

private static HashMap<String, String> messageDrafts = new HashMap<String, String>();
Expand Down Expand Up @@ -303,13 +306,14 @@ public void runPermissionAction(int request_id) {
switch (request_id) {
case REQUEST_CAMERA:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempFile = FileHelper.createImageFile(this);
if (tempFile == null) {
cameraPictureFile = FileHelper.createImageFile(this);
if (cameraPictureFile == null) {
Toast.makeText(this, "Error creating file!", Toast.LENGTH_SHORT).show();
return;
}
cameraPictureUri = Uri.fromFile(tempFile);
// TODO: wrap file Uri into a FileProvider to target Android M+
cameraPictureUri = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider.Files",
cameraPictureFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPictureUri);
startActivityForResult(Intent.createChooser(intent, getString(R.string.roster_contextmenu_take_image)), REQUEST_CAMERA);
return;
Expand Down Expand Up @@ -349,7 +353,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
sendFile(uri, flags);
}
} else if (requestCode == REQUEST_CAMERA && cameraPictureUri != null) {
sendFile(cameraPictureUri, FileHttpUploadTask.F_RESIZE);
sendFile(Uri.fromFile(cameraPictureFile), FileHttpUploadTask.F_RESIZE);
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/org/yaxim/androidclient/util/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,7 @@ public static File createImageFile(Context ctx) {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + "_";
File storageDir = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
storageDir = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
}
if (storageDir == null)
storageDir = new File(Environment.getExternalStorageDirectory() + "/DCIM/");
File storageDir = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
try {
File image = File.createTempFile(
imageFileName, /* prefix */
Expand Down

0 comments on commit a1dc9a8

Please sign in to comment.