I call camera to take a picture. But I cannot go back to my own original activity after taking the picture. What's the problem? Thank you.
public void addEntry(View view)
{
String EntryName=RegisterName.toString();
Toast.makeText(this, EntryName, Toast.LENGTH_LONG);
Intent addEntryintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(getFilesDir(),EntryName);
registeryFileUri = Uri.fromFile(file);
addEntryintent.putExtra(MediaStore.EXTRA_OUTPUT, registeryFileUri);
startActivityForResult(addEntryintent,TAKE_PICTURE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE)
{
if (data != null)
{
Toast.makeText(this, "Successfully Registered!", Toast.LENGTH_LONG);
ImageView Registerimage= (ImageView)findViewById(R.id.RegisterPicture);
Registerimage.setImageURI(registeryFileUri);
}
}
}
It took me a while to get it working and I have made several things and finally it works. I can't tell certainly which of the things I did is the solution to the problem, but they all together form the working solution.
There are multiple reasons why the camera activity does not return back. Two major ones are:
path for the new picture is invalid, or non-existing, or it can't be created
application got suspended and saved path get lost.
So here is my code solving all these problems, all together working.
First I created helper class ImageServices:
class ImageServices {
private static String getTempDirectoryPath(Context ctx) {
File cache;
// SD Card Mounted
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/" + ctx.getPackageName() + "/cache/");
}
// Use internal storage
else {
cache = ctx.getCacheDir();
}
// Create the cache directory if it doesn't exist
if (!cache.exists()) {
cache.mkdirs();
}
return cache.getAbsolutePath();
}
public static Uri getOutputImageFileUri(Context ctx) {
// TODO: check the presence of SDCard
String tstamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = new File(getTempDirectoryPath(ctx), "IMG_" + tstamp + ".jpg");
return Uri.fromFile(file);
}
}
The code is partially inspired by developer.android.com and partially by CameraLauncher class of Apache Cordova project.
In my activity the event handler for button to take a picture looks like this:
private Uri imageFileUri;
private static final int MAKE_PHOTO_RESULT_CODE = 100;
private static final int PICK_PHOTO_RESULT_CODE = 101;
public void onMakePhoto(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageFileUri = ImageServices.getOutputImageFileUri(this);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
Log.i("babies", "Taking picture: requested " + imageFileUri);
startActivityForResult(intent, MAKE_PHOTO_RESULT_CODE);
}
Method onActivityResult does not really contain much, as imageFileUri already points to the existing file and necessary rendering is done in onResume method, which is called when the activity gets back into foreground:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode) {
case MAKE_PHOTO_RESULT_CODE:
assert imageFileUri != null;
break;
case ...
...other cases...
break;
}
}
}
But this is still not sufficient, as imageFileUri gets lost as your app gets suspended. And on regular device the chances are close to 100%. So next you need to store the value of imageFileUri to instance state:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (imageFileUri == null) {
outState.putString("file-uri", "");
}
else {
outState.putString("file-uri", imageFileUri.toString());
}
};
and load it again in - easiest is directly in onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState != null) {
String fileUri = savedInstanceState.getString("file-uri");
if (!fileUri.equals("")) imageFileUri = Uri.parse(fileUri);
}
}
So, again, on top of many other solutions presented on this site as well as elsewhere, there are two major differences:
smarter getTempDirectoryPath inspired by Apache Cordova
allowing imageFileUri to survive suspended application
And now - at least for me - everything works fine.
Answer
Use appContext.getExternalCacheDir() and don't forget to mention permissons.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE)
{
if(resultCode==Activity.RESULT_OK)
{ //if (data != null)
//{
Toast.makeText(this, "Successfully Registered!", Toast.LENGTH_LONG);
ImageView Registerimage= (ImageView)findViewById(R.id.RegisterPicture);
Registerimage.setImageURI(registeryFileUri);
//}
}
else
Toast.makeText(this, "Not Registered!", Toast.LENGTH_LONG);
}
**"android.permission.CAMERA"**
Check whether the above permission is specified in your manifest or not
Note: It's better to use getExternalCacheDir() than getFilesDir() if you still dont get the
image then use that. Dont forgot to specify the permission "android.permission.WRITE_EXTERNAL_STORAGE" if you use the getExternalCacheDir().
On some devices data will unfortunately be null in onActivityResult after calling the camera activity. So you may need to save your state in your activity's variables, and them read them in onActivityResult. Be sure to save these variables in onSaveInstanceState and restore them in onCreate.
Related
I'm new here. I discover how StackOverflow works.
I'm creating a new android homescreen widget. My widget has a button. When pressed, it starts an activity (it's not a configure Activity, just a standard activity). In this activity, I have a test button. My purpose is to create a text file after pressing this test button.
In onCreate function, I have this code to handle the button :
final Button testButton = findViewById(R.id.button_test);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ActivityCompat.requestPermissions(WidgetActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
Utils.MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
String fileName = "test.txt";
Intent exportIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
exportIntent.addCategory(Intent.CATEGORY_OPENABLE);
exportIntent.setType("text/plain");
exportIntent.putExtra(Intent.EXTRA_TITLE, fileName);
startActivityForResult(exportIntent, FILE_EXPORT_REQUEST_CODE);
}
});
And I have this function :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case FILE_EXPORT_REQUEST_CODE:
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Context c = WidgetActivity.this;
ParcelFileDescriptor pfd = null;
try {
pfd = c.getContentResolver().openFileDescriptor(uri, "w");
Preferences.export(mAppWidgetId, pfd.getFileDescriptor(), WidgetActivity.this);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
break;
}
}
onActivityResult is never called.
I put this code in my MainActivity, and it works very well.
I don't know how to achieve this...
It's seems to be impossible.
The way I achieve this :
My widget starts its activity (not the MainActivity), as usual, and one feature offer the possibility to create a file.
The button in this activity create a new intent with action code, and launch MainActivity.
MainActivity use action code to decide what to do : execute code to create the file.
MainActivity have a onActivityResult called when user choose a file name and a directory.
I made an OCR application that makes a screenshot using Android mediaprojection and processes the text in this image. This is working fine, except on Android 9+. When mediaprojeciton is starting there is always a window popping up warning about sensitive data that could be recorded, and a button to cancel or start recording. How can I achieve that this window will only be showed once?
I tried preventing it from popping up by creating two extra private static variables to store intent and resultdata of mediaprojection, and reusing it if its not null. But it did not work (read about this method in another post).
// initializing MP
mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
// Starting MediaProjection
private void startProjection() {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}
// OnActivityResult
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (requestCode == 100) {
if(mProjectionManager == null) {
cancelEverything();
return;
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(mProjectionManager != null)
sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
else
cancelEverything();
if (sMediaProjection != null) {
File externalFilesDir = getExternalFilesDir(null);
if (externalFilesDir != null) {
STORE_DIRECTORY = externalFilesDir.getAbsolutePath() + "/screenshots/";
File storeDirectory = new File(STORE_DIRECTORY);
if (!storeDirectory.exists()) {
boolean success = storeDirectory.mkdirs();
if (!success) {
Log.e(TAG, "failed to create file storage directory.");
return;
}
}
} else {
Log.e(TAG, "failed to create file storage directory, getExternalFilesDir is null.");
return;
}
// display metrics
DisplayMetrics metrics = getResources().getDisplayMetrics();
mDensity = metrics.densityDpi;
mDisplay = getWindowManager().getDefaultDisplay();
// create virtual display depending on device width / height
createVirtualDisplay();
// register orientation change callback
mOrientationChangeCallback = new OrientationChangeCallback(getApplicationContext());
if (mOrientationChangeCallback.canDetectOrientation()) {
mOrientationChangeCallback.enable();
}
// register media projection stop callback
sMediaProjection.registerCallback(new MediaProjectionStopCallback(), mHandler);
}
}
}, 2000);
}
}
My code is working fine on Android versions below Android 9. On older android versions I can choose to keep that decision to grant recording permission, and it will never show up again. So what can I do in Android 9?
Thanks in advance, I'm happy for every idea you have :)
Well the problem was that I was calling
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
every time, which is not necessary (createScreenCaptureIntent() leads to the dialog window which requests user interaction)
My solution makes the dialog appear only once (if application was closed it will ask for permission one time again).
All I had to do was making addiotional private static variables of type Intent and int.
private static Intent staticIntentData;
private static int staticResultCode;
On Activity result I assign those variables with the passed result code and intent:
if(staticResultCode == 0 && staticIntentData == null) {
sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
staticIntentData = data;
staticResultCode = resultCode;
} else {
sMediaProjection = mProjectionManager.getMediaProjection(staticResultCode, staticIntentData)};
}
Every time I call my startprojection method, I will check if they are null:
if(staticIntentData == null)
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
else
captureScreen();
If null it will request permission, if not it will start the projection with the static intent data and static int resultcode, so it is not needed to ask for that permission again, just reuse what you get in activity result.
sMediaProjection = mProjectionManager.getMediaProjection(staticResultCode, staticIntentData);
Simple as that! Now it will only showing one single time each time you use the app. I guess thats what Google wants, because theres no keep decision checkbox in that dialog like in previous android versions.
These codes can only capture and display image I took, and when I pressed save button, it only saves "Note title" onto my list View. And when I clicked "Note Title" it doesn't display the picture anymore. What should I add to save it and update it? Thanks.
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
imageviewid.setImageBitmap(mphoto);
}
}
public void takeImageFromCamera(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
private void saveNote(){
fillNameIfEmpty();
id = DbAccess.addNote(getBaseContext(), etName.getText().toString(), mFileName, DbContract.NoteEntry.TYPE_PHOTO, currentCat);
Toast.makeText(getApplicationContext(), R.string.toast_saved, Toast.LENGTH_SHORT).show();
}
private void updateNote(){
fillNameIfEmpty();
DbAccess.updateNote(getBaseContext(), id, etName.getText().toString(), mFileName, currentCat);
Toast.makeText(getApplicationContext(), R.string.toast_updated, Toast.LENGTH_SHORT).show();
}
Your DbAccess API doesn't know anything about an image in the note!
So I would extend your code to:
After picture is taken it saved in fail in application sandbox
Save filename of image assigned to note to database together with note
And don't forget to handle editing or deletion picture use cases!
I really have no idea how to do it like this, the note is able to attach photo from camera and gallery
Also the other functions like shown on the upper part, I haven't found any tutorial about it. I need help, thank you. I'm a beginner
The scope of this problem may be a bit broad, but I'll try to summarize it as best as I can.
In order to render images from say, a user's third-party gallery app, you'd need to access their device storage first by initially setting the storage permission in your manifest as follows:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Next, you have to address granting permissions (accessing the user's device storage is considered a dangerous permission) accordingly for Android 6.0/Marshmallow and above at runtime (during installation for Android 6.0 and below) prior to running any UI threads as mentioned here in the docs. Then you open up the gallery app from say, clicking on a Button, and then render an ImageView with bitmap to the URI path of the selected image by using the storage data via a Cursor all within onActivityResult().
Here's a sample Activity of just that:
public class MainActivity extends AppCompatActivity {
// Constant that's used as a parameter to assist with the permission requesting process.
private static final int PERMISSION_CODE = 100;
// Int constant that's used to handle the result back when an image is selected from the
// device's gallery.
private static final int RESULT_LOAD_IMAGE = 1;
private ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Requests permission for devices with versions Marshmallow (M)/API 23 or above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_CODE);
return;
}
}
// The following invoking method either executes for versions older than M, or until the
// user accepts the in-app permission for the next sessions.
runUi();
}
// Displays a permission dialog when requested for devices M and above.
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSION_CODE) {
// User accepts the permission(s).
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Invoker for rendering UI.
runUi();
} else { // User denies the permission.
Toast.makeText(this, "Please come back and then grant permissions!",
Toast.LENGTH_SHORT).show();
// Runs a thread for a slight delay prior to shutting down the app.
Thread mthread = new Thread() {
#Override
public void run() {
try {
sleep(1500);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
mthread.start();
}
}
}
private void runUi() {
mImageView = (ImageView) findViewById(R.id.image_view);
// Sets the image button clickable with the following functionality.
findViewById(R.id.change_img_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Instantiates an Intent object for accessing the device's storage.
Intent intent = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Triggers the image gallery.
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
}
/**
* Invoked once a third-party app (such as Gallery) is dismissed from its purpose via an
* implicit intent.
*
* #param requestCode is the code constant of the intent's purpose.
* #param resultCode is the result code constant of the intent.
* #param data is the actual intent.
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Runs the following code should the code constants and intent match that of selecting an
// image from the device's gallery.
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
// References the device's storage URI for images from the intent parameter.
Uri selectedImageUri = data.getData();
// Initializes a temporary string list of the image file path as the column to render
// the image immediately.
String[] projection = { MediaStore.Images.Media.DATA };
// References and queries the database with the following parameters, and then moves to
// the first row index.
Cursor cursor = getContentResolver().query(
selectedImageUri, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
null); // Default sort order
cursor.moveToFirst();
// Retrieves and assigns the file path as a string value, and then sets the image's
// bitmap to render it.
String imgFilePath = cursor.getString(cursor.getColumnIndex(projection[0]));
mImageView.setImageBitmap(BitmapFactory.decodeFile(imgFilePath));
// Closes the cursor to release all of its resources.
cursor.close();
}
}
}
I am making a activity whereby i ask the user to load image from various sources and showing the selected image on a imageview in a different activity.
I am able to show the image from the camera and gallery but from dropbox it is showing error.
For Camera and gallery i can even query the uri obtained from
intent.getData()
in the onactivityresult method and obtain the Filepath and accordingly even obtain the bitmap and resize it .
But the same is not working for Dropbox. Kindly update what code to use for Dropbox so that all options start working.
thanks
For Dropbox you will need to use their Android Chooser to choose files from the user's Dropbox account. You will need a Dropbox API key for this. Once you have grabbed the chooser library and your API key it should be fairly easy to implement;
private Button mChooserButton;
private DbxChooser mChooser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChooser = new DbxChooser(APP_KEY);
mChooserButton = (Button) findViewById(R.id.chooser_button);
mChooserButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mChooser.forResultType(DbxChooser.ResultType.PREVIEW_LINK)
.launch(MainActivity.this, DBX_CHOOSER_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DBX_CHOOSER_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
DbxChooser.Result result = new DbxChooser.Result(data);
Log.d("main", "Link to selected file: " + result.getLink());
// Handle the result
} else {
// Failed or was cancelled by the user.
}
} else if (requestCode == GALLERY) {
// If your request was from the user gallery
Log.d("main", "Link to selected file: " + data.getData());
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}