-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
QFieldGalleryPictureActivity.java
96 lines (82 loc) · 3.28 KB
/
QFieldGalleryPictureActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package ch.opengis.qfield;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import androidx.core.content.FileProvider;
import androidx.documentfile.provider.DocumentFile;
import ch.opengis.qfield.QFieldUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class QFieldGalleryPictureActivity extends Activity {
private static final String TAG = "QField Gallery Picture Activity";
private static final int GALLERY_ACTIVITY = 173;
private String prefix;
private String pictureFilePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (!intent.hasExtra("prefix") || !intent.hasExtra("pictureFilePath")) {
Log.d(TAG, "QFieldGalleryPictureActivity missing extras");
finish();
return;
}
prefix = intent.getExtras().getString("prefix");
pictureFilePath = intent.getExtras().getString("pictureFilePath");
Log.d(TAG, "Received prefix: " + prefix +
" and pictureFilePath: " + pictureFilePath);
callGalleryIntent();
return;
}
private void callGalleryIntent() {
Log.d(TAG, "callGalleryIntent()");
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, GALLERY_ACTIVITY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
Log.d(TAG, "onActivityResult()");
Log.d(TAG, "resultCode: " + resultCode);
if (requestCode == GALLERY_ACTIVITY) {
if (resultCode == RESULT_OK) {
File result = new File(prefix + pictureFilePath);
Log.d(TAG, "Selected picture: " + data.getData().toString());
try {
Uri uri = data.getData();
DocumentFile documentFile = DocumentFile.fromSingleUri(
getApplication().getApplicationContext(), uri);
InputStream in = getContentResolver().openInputStream(uri);
QFieldUtils.inputStreamToFile(in, result.getPath(),
documentFile.length());
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
Intent intent = this.getIntent();
intent.putExtra("PICTURE_IMAGE_FILENAME",
prefix + pictureFilePath);
setResult(RESULT_OK, intent);
} else {
Intent intent = this.getIntent();
intent.putExtra("PICTURE_IMAGE_FILENAME", "");
setResult(RESULT_CANCELED, intent);
}
finish();
}
}
}