Created
January 20, 2021 17:21
-
-
Save tneotia/adc61196cfa59a5a7bdef6efb0ab5883 to your computer and use it in GitHub Desktop.
newActivity.java file for starting camera/file chooser intent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.flutter.plugins.webviewflutter; | |
import android.app.Activity; | |
import android.app.Dialog; | |
import android.content.ClipData; | |
import android.content.ComponentName; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.content.pm.ResolveInfo; | |
import android.content.res.AssetFileDescriptor; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.os.Parcelable; | |
import android.provider.MediaStore; | |
import android.util.Log; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.webkit.ValueCallback; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class newActivity extends Activity { | |
private static ValueCallback<Uri[]> mUploadMessages; | |
private Object Utils; | |
private File outputFile; | |
private Uri mOutputFileUri; | |
private Uri image; | |
public static void getfilePathCallback(ValueCallback<Uri[]> filePathCallback){ | |
mUploadMessages = filePathCallback; | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | |
openImageIntent(); | |
} | |
private Uri outputFileUri; | |
private void openImageIntent() { | |
try { | |
outputFile = File.createTempFile("tmp", ".jpg", getCacheDir()); | |
} catch (IOException pE) { | |
pE.printStackTrace(); | |
} | |
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FolderName"); | |
if (!imageStorageDir.exists()) { | |
imageStorageDir.mkdirs(); | |
} | |
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); | |
mOutputFileUri = Uri.fromFile(file); | |
// Camera. | |
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mOutputFileUri); | |
// Filesystem. | |
final Intent fileIntent = new Intent(); | |
fileIntent.setType("*/*"); | |
fileIntent.setAction(Intent.ACTION_OPEN_DOCUMENT); | |
fileIntent.addCategory(Intent.CATEGORY_OPENABLE); | |
// Chooser of filesystem options. | |
final Intent chooserIntent = Intent.createChooser(fileIntent, "Select Source"); | |
// Add the camera options. | |
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent}); | |
startActivityForResult(chooserIntent, 42); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { | |
super.onActivityResult(requestCode, resultCode, intent); | |
Uri[] results = null; | |
try { | |
if (resultCode != RESULT_OK) { | |
results = null; | |
} else { | |
if (intent != null) { | |
String dataString = intent.getDataString(); | |
ClipData clipData = intent.getClipData(); | |
if (clipData != null) { | |
results = new Uri[clipData.getItemCount()]; | |
for (int i = 0; i < clipData.getItemCount(); i++) { | |
ClipData.Item item = clipData.getItemAt(i); | |
results[i] = item.getUri(); | |
} | |
} | |
if (dataString != null) { | |
results = new Uri[]{Uri.parse(dataString)}; | |
} | |
} else { | |
results = new Uri[]{mOutputFileUri}; | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
mUploadMessages.onReceiveValue(results); | |
mUploadMessages = null; | |
finish(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment