Skip to content

Commit 53cc426

Browse files
author
Honza Rychnovský
committedSep 4, 2018
Improve addToGallery mechanism
1 parent 56fc499 commit 53cc426

File tree

2 files changed

+61
-46
lines changed

2 files changed

+61
-46
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.honzar.adtutils.sampleApp;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.support.v7.app.AppCompatActivity;
56

6-
import com.google.gson.JsonObject;
7-
import com.honzar.adtutils.library.*;
7+
import com.honzar.adtutils.library.IntentUtils;
88

99
import java.io.File;
1010
import java.io.IOException;
11-
import java.util.Date;
1211

1312
public class MainActivity extends AppCompatActivity {
1413

@@ -17,46 +16,59 @@ protected void onCreate(Bundle savedInstanceState) {
1716
super.onCreate(savedInstanceState);
1817
setContentView(R.layout.activity_main);
1918

20-
JsonObject o = new JsonObject();
21-
o.addProperty("test", true);
22-
Boolean b = JsonUtils.getBoolean(o, "test");
23-
24-
LogUtils.d(com.honzar.adtutils.sampleApp.BuildConfig.DEBUG, MainActivity.class.getName(), "test: " + b);
25-
26-
IntentUtils.openBrowserApp(MainActivity.this, "http://www.appsdevteam.com/?ref=mujsvetzdraviandroidapp");
27-
28-
LogUtils.d(com.honzar.adtutils.sampleApp.BuildConfig.DEBUG, MainActivity.class.getName(), "test navigation bar height: " + ViewUtils.getSystemNavigationBarHeight(MainActivity.this));
29-
LogUtils.d(com.honzar.adtutils.sampleApp.BuildConfig.DEBUG, MainActivity.class.getName(), "test status bar height: " + ViewUtils.getSystemStatusBarHeight(MainActivity.this));
30-
31-
LogUtils.d(true, "test", DateTimeUtils.getFormattedDateAndTimeInWordsWithSystemLocale(MainActivity.this, new Date()));
32-
33-
ViewUtils.keepScreenOn(MainActivity.this, true);
34-
35-
LogUtils.d(true, "TEST DATE RANGE", DateTimeUtils.getFormattedDateAndTimeIntervalWithSystemLocale(MainActivity.this, new Date(1509959820000L), new Date(1510136220000L)));
36-
LogUtils.d(true, "TEST DATE RANGE", DateTimeUtils.getFormattedDateAndTimeIntervalWithSystemLocale(MainActivity.this, new Date(1509963420000L), new Date(1509992820000L)));
37-
38-
LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 1510515505193L, true));
39-
LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 1510515505193L, false));
40-
LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 13243243, true));
41-
LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 13243243, false));
42-
43-
LogUtils.d(true, "TEST MD5 hash input", "123456789qwerty");
44-
LogUtils.d(true, "TEST MD5 hash output", HashingUtils.md5("123456789qwerty"));
45-
46-
//ViewUtils.setStatusBarColorIfCan(MainActivity.this, R.color.colorAppWhite);
47-
//ViewUtils.setNavigationBarColorIfCan(MainActivity.this, R.color.colorAppWhite);
48-
49-
LogUtils.d(true, "Network type", VersionUtils.getNetworkTypeName(MainActivity.this));
50-
51-
//IntentUtils.openFilePickerComponent(MainActivity.this,"video/*", "TEST TITLE", 101);
52-
53-
File file = new File(getExternalFilesDir(null), "image.jpeg");
19+
// JsonObject o = new JsonObject();
20+
// o.addProperty("test", true);
21+
// Boolean b = JsonUtils.getBoolean(o, "test");
22+
//
23+
// LogUtils.d(com.honzar.adtutils.sampleApp.BuildConfig.DEBUG, MainActivity.class.getName(), "test: " + b);
24+
//
25+
// IntentUtils.openBrowserApp(MainActivity.this, "http://www.appsdevteam.com/?ref=mujsvetzdraviandroidapp");
26+
//
27+
// LogUtils.d(com.honzar.adtutils.sampleApp.BuildConfig.DEBUG, MainActivity.class.getName(), "test navigation bar height: " + ViewUtils.getSystemNavigationBarHeight(MainActivity.this));
28+
// LogUtils.d(com.honzar.adtutils.sampleApp.BuildConfig.DEBUG, MainActivity.class.getName(), "test status bar height: " + ViewUtils.getSystemStatusBarHeight(MainActivity.this));
29+
//
30+
// LogUtils.d(true, "test", DateTimeUtils.getFormattedDateAndTimeInWordsWithSystemLocale(MainActivity.this, new Date()));
31+
//
32+
// ViewUtils.keepScreenOn(MainActivity.this, true);
33+
//
34+
// LogUtils.d(true, "TEST DATE RANGE", DateTimeUtils.getFormattedDateAndTimeIntervalWithSystemLocale(MainActivity.this, new Date(1509959820000L), new Date(1510136220000L)));
35+
// LogUtils.d(true, "TEST DATE RANGE", DateTimeUtils.getFormattedDateAndTimeIntervalWithSystemLocale(MainActivity.this, new Date(1509963420000L), new Date(1509992820000L)));
36+
//
37+
// LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 1510515505193L, true));
38+
// LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 1510515505193L, false));
39+
// LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 13243243, true));
40+
// LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 13243243, false));
41+
//
42+
// LogUtils.d(true, "TEST MD5 hash input", "123456789qwerty");
43+
// LogUtils.d(true, "TEST MD5 hash output", HashingUtils.md5("123456789qwerty"));
44+
//
45+
// //ViewUtils.setStatusBarColorIfCan(MainActivity.this, R.color.colorAppWhite);
46+
// //ViewUtils.setNavigationBarColorIfCan(MainActivity.this, R.color.colorAppWhite);
47+
//
48+
// LogUtils.d(true, "Network type", VersionUtils.getNetworkTypeName(MainActivity.this));
49+
//
50+
// //IntentUtils.openFilePickerComponent(MainActivity.this,"video/*", "TEST TITLE", 101);
51+
52+
file = new File(getExternalFilesDir(null), "image.mp4");
5453
try {
5554
file.createNewFile();
5655
} catch (IOException e) {
5756
e.printStackTrace();
5857
}
5958

60-
IntentUtils.openCameraAppForPhoto(MainActivity.this, -1, false, file, true);
59+
IntentUtils.openCameraAppForPhoto(MainActivity.this, 2209, false, file, true);
60+
}
61+
File file;
62+
63+
@Override
64+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
65+
super.onActivityResult(requestCode, resultCode, data);
66+
67+
if (requestCode == 2209) {
68+
69+
if (resultCode == RESULT_OK) {
70+
String filePath = file.getAbsolutePath();
71+
}
72+
}
6173
}
6274
}

‎library/src/main/java/com/honzar/adtutils/library/IntentUtils.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,7 @@ public static boolean openCameraAppForPhoto(Context context, int requestCode, bo
204204

205205
if (addToGallery) {
206206
takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
207-
ContentValues values = new ContentValues();
208-
values.put(MediaStore.Images.Media.TITLE, outputFile.getName());
209-
Uri mCapturedImageURI = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
210-
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
207+
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, addFileToGallery(context, outputFile, "image/*"));
211208
} else {
212209
if (VersionUtils.isThisDeviceNougatAndHigher()) {
213210
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", outputFile));
@@ -260,10 +257,7 @@ public static boolean openCameraAppForVideo(Context context, int requestCode, bo
260257

261258
if (addToGallery) {
262259
takeVideoIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
263-
ContentValues values = new ContentValues();
264-
values.put(MediaStore.Images.Media.TITLE, outputFile.getName());
265-
Uri mCapturedImageURI = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
266-
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
260+
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, addFileToGallery(context, outputFile, "video/*"));
267261
} else {
268262
if (VersionUtils.isThisDeviceNougatAndHigher()) {
269263
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", outputFile));
@@ -281,6 +275,15 @@ public static boolean openCameraAppForVideo(Context context, int requestCode, bo
281275
return false;
282276
}
283277

278+
private static Uri addFileToGallery(Context context, File file, String mimeType)
279+
{
280+
ContentValues values = new ContentValues(3);
281+
values.put(MediaStore.Video.Media.TITLE, file.getName());
282+
values.put(MediaStore.Video.Media.MIME_TYPE, mimeType);
283+
values.put(MediaStore.Video.Media.DATA, file.getAbsolutePath());
284+
return context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
285+
}
286+
284287
/**
285288
* Opens calendar app and add new event
286289
*

0 commit comments

Comments
 (0)
Please sign in to comment.