How to load an image having its URI? - android

I'm having a really hard time trying to figure out how to load an image when I have its URI.
The app works in the following manner:
I'm checking in the SQLite database whether a user exists and then I'm starting DescriptionActivity (code below). If the user had already been added to database I want to get the description from the database and load it into EditText (descFromDb[0] - no problem here) and get the URI from database in order to load the image into ImageView component (descFromDb[1] - lots of problems here), the logs from the Logcat are pasted below the class code. There's also some code for the case when user wants to upload a pic from the gallery and a button that updates the description and image URI in database.
I already added WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE and MANAGE_DOCUMENTS permissions to my manifest file however this still doesn't allow me to load the image based on its URI.
I read some threads that advised to use Intents and write code in a similar manner to how I set the image from gallery but tbh I don't really have a lot of experience in use of Intents other than starting a new Activity with layout added to it and those examples were quite confusing.
TL;DR I need to set the picture to ImageView while only having its URI - image.setImageURI(imgUri); doesn't work.
package com.example.kuba.myapplication;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.List;
import static com.example.kuba.myapplication.MainActivity.kidDatabase;
public class DescriptionActivity extends AppCompatActivity {
public static final int PICK_IMAGE = 1;
private static final String KIDS_DB = "kids_db";
private int kidId;
EditText descText;
Button imgBtn;
Button updateBtn;
ImageView image;
Uri imgUri = null;
Button refreshBtn;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE) {
imgUri = data.getData();
image.setImageURI(imgUri);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_desc);
kidId = getIntent().getIntExtra("KID_ID", -1);
image = (ImageView) findViewById(R.id.imageView);
imgBtn = (Button) findViewById(R.id.picBtn);
refreshBtn = (Button) findViewById(R.id.refreshBtn);
descText = (EditText) findViewById(R.id.descEditText);
final String[] descFromDb = new String[2];
new Thread(new Runnable() {
#Override
public void run() {
descFromDb[0] = kidDatabase.daoAccess().getDesc(kidId);
descFromDb[1] = kidDatabase.daoAccess().getImgPath(kidId);
if (descFromDb[0] != null) {
descText.setText(descFromDb[0]);
}
if (descFromDb[1] != null) {
imgUri = Uri.parse(descFromDb[1]);
image.setImageURI(null);
image.setImageURI(imgUri);
}
}
}).start();
refreshBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
image.setImageURI(imgUri);
}
});
imgBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
updateBtn = (Button) findViewById(R.id.saveBtn);
updateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new Thread(new Runnable() {
#Override
public void run() {
Kid kid = kidDatabase.daoAccess().fetchKidById(kidId);
if (kid == null) {
return;
} else {
String description = descText.getText().toString();
kidDatabase.daoAccess().updateDescAndPath(kidId, description, imgUri.toString());
}
}
}).start();
Toast.makeText(getApplicationContext(), "Desc updated", Toast.LENGTH_LONG).show();
}
});
}
}
04-28 15:02:23.264 16941-17042/com.example.kuba.myapplication I/WAR:: imguri: content://com.android.providers.media.documents/document/image%3A47
04-28 15:02:23.267 16941-17042/com.example.kuba.myapplication W/ImageView: Unable to open content: content://com.android.providers.media.documents/document/image%3A47
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{fbe1f29 16941:com.example.kuba.myapplication/u0a84} (pid=16941, uid=10084) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4199)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5476)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2239)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1517)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1131)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:984)
at android.content.ContentResolver.openInputStream(ContentResolver.java:704)
at android.widget.ImageView.getDrawableFromUri(ImageView.java:900)
at android.widget.ImageView.resolveUri(ImageView.java:871)
at android.widget.ImageView.setImageURI(ImageView.java:490)
at android.support.v7.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:115)
at com.example.kuba.myapplication.DescriptionActivity$1.run(DescriptionActivity.java:75)
at java.lang.Thread.run(Thread.java:761)
resolveUri failed on bad bitmap uri: content://com.android.providers.media.documents/document/image%3A47
04-28 15:02:23.378 16941-16946/com.example.kuba.myapplication I/art: Do partial code cache collection, code=60KB, data=59KB
After code cache collection, code=60KB, data=59KB
Increasing code cache capacity to 256KB

Just use an image loading library, for example Glide, add the dependency:
implementation "com.github.bumptech.glide:glide:4.7.1"
It must be compiled against compile SDK version v27 or higher. And it uses support library v27, if you want to use a lower version of the support library then refer here
And finally the usage:
Glide.with(this)
.load(url)
.into(imageView);

Related

How to start gallery app from an android app?

I am trying to open the gallery app from my android application. I want to just open the gallery app and not other apps with like fotos.
I have tried
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
and
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");
and
intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
The problem is that all this show a dialog where I can pick again from all apps that have images. So it shows a dialog with "Gallery", "Fotos", etc....
Is there a possibility to open just one app, without having to chose again?
EDIT: If you want to use the gallery API, which does show photos from all photo apps on the phone and which is not a standalone user-accessible app itself, you could use this code:
private static final int PICK_IMAGE = 100;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_picker);
imageView = (ImageView) findViewById(R.id.image_view);
Button pickImageButton = (Button) findViewById(R.id.pick_button);
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() {
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
Unfortunately, I don't believe choosing photos from a specific app is possible, as "the gallery app" that comes installed on devices varies by manufacturer and OS skin. For example, the default gallery on Pixel devices is actually Google Photos.
If I misunderstood, and you do want to simply launch the Gallery app, you can include the following code in your /src/[ActivityName].java file:
package com.example.andy.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
final LinearLayout parent = findViewById(R.id.parent);
textView = findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.sec.android.gallery3d");
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(MainActivity.this, "There is no package available in android", Toast.LENGTH_LONG).show();
}
}
});
}
}
Notice that the app being opened is Gallery, the default Samsung photo gallery app. If you want to replace this with Google Photos, you would replace com.sec.android.gallery3d with com.google.android.apps.photos.

SAF - listening to events from a folder in the cloud

I am experimenting with Storage Access Framework, because I would like to improve my app, that has migrated to SAF already (although I had to surrender some features because of some SAF design flaws or maybe bugs).
Now I want that my app is notified when the user does something in a SAF folder in the cloud.
At present time it is purely experimental because it is by hack that it is possible to have the uri of a cloud folder by GDrive.
(the other cloud providers seem not to be SAF ready, please correct me if I am wrong)
I am just working in advance before they release the full feature (there is a bugfix request about that) to be able to select a folder on a cloud storage root in the system picker.
I created a simple application for creating a SAF folder on the cloud (selection is not possible at present time on cloud storage) and listening to events.
package com.example.safevents;
import android.app.Activity;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
Activity activity;
Handler handler;
Observer observer;
class Observer extends ContentObserver {
public Observer(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange) {
this.onChange(selfChange, null);
Log.d("SAF","event fired; selfchange="+selfChange);
}
#Override
public void onChange(boolean selfChange, Uri uri) {
Log.d("SAF","event fired on uri="+uri.toString()+"; selfchange="+selfChange);
}
}
public void openPickerForFolderCreation(Activity activity, int requestCode) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.setType("vnd.android.document/directory");
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
activity.startActivityForResult(intent, requestCode);
}
public Uri takePermanentReadWritePermissions(Activity activity, Uri uri, int flags) {
int takeFlags = flags
&
(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
);
;
ContentResolver resolver = activity.getContentResolver();
resolver.takePersistableUriPermission(uri, takeFlags);
return uri;
}
public boolean arePermissionsGranted(Activity activity, String uriString) {
Uri uri = Uri.parse(uriString);
ContentResolver resolver = activity.getContentResolver();
List<UriPermission> list = resolver.getPersistedUriPermissions();
for (int i = 0; i < list.size(); i++) {
if (((Uri.decode(list.get(i).getUri().toString())).equals(Uri.decode(uriString))) && list.get(i).isWritePermission() && list.get(i).isReadPermission()) {
return true;
}
}
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
activity=this;
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
SAFUtils.openPickerForFolderCreation(activity,0);
}
});
}
#Override
protected void onDestroy()
{
super.onDestroy();
//important
getContentResolver().unregisterContentObserver(observer);
}
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent returnedIntent)
{
Log.d("SAF","picker returned "+resultCode+" "+requestCode);
if (returnedIntent!=null) Log.d("SAF","intent="+returnedIntent.toString());
if (returnedIntent.getData()!=null) Log.d("SAF","uri="+returnedIntent.getData().toString());
//resultCode is -1
if (resultCode==-1)
if (requestCode==0)
{
Uri uri=SAFUtils.takePermanentReadWritePermissions(activity,returnedIntent.getData(),returnedIntent.getFlags());
Log.d("SAF","read/write permissions="+SAFUtils.arePermissionsGranted(activity,uri.toString()));
handler=new Handler(){};
observer=new Observer(handler);
getContentResolver().
registerContentObserver(
uri,
true,
observer);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I get this log lines:
com.example.safevents D/SAF: picker returned -1 0
com.example.safevents D/SAF: intent=Intent { dat=content://com.google.android.apps.docs.storage/document/acc=3;doc=encoded=randomalphanumericsequence= flg=0x43 }
com.example.safevents D/SAF: uri=content://com.google.android.apps.docs.storage/document/acc%3D3%3Bdoc%3Dencoded%3Dthesamealphanumericsequence%3D
com.example.safevents D/SAF: read/write permissions=true
but I do not receive any event notifications after some simple file operations inside the created folder.
Note that resultCode= -1.
Also note that omitting the call to the takePersistentPermissions method leads to no permissions, resulting from the utility function that check them not finding the uri in the list.
Is there something wrong in the code above?
Is the creation of SAF cloud folder such a hack that it does not work?
Is a further hack possible to force this to work so to be tested roughly in advance?
Is that SAF functionality not available at present time?

FileObserver not working with intent

I need to use camera intent and keep track of the new images taken by the intent. As I need to take multiple photos at a time, I used the intent MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA. Here is my code:
package com.arko.cameraintent;
import android.content.Intent;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.File;
public class CameraMainActivity extends AppCompatActivity {
private static final int REQ_CODE = 0;
AppCompatActivity main_activity = this; // save it for future use
DirectoryFileObserver directoryFileObserver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_main);
}
public void takePhoto(View view) // fired when a button in the main application is clicked. This starts the camera application.
{
Intent callCamAppIntent = new Intent();
callCamAppIntent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
Toast.makeText(this, dir, Toast.LENGTH_SHORT).show();
directoryFileObserver = new DirectoryFileObserver(dir);
directoryFileObserver.startWatching();
startActivityForResult(callCamAppIntent, REQ_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_CODE){
Toast.makeText(this, "picture taken successfully", Toast.LENGTH_SHORT).show();
directoryFileObserver.stopWatching();
}
}
}
The code of DirectoryFileObserver class:
package com.arko.cameraintent;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.FileObserver;
import android.support.annotation.Nullable;
import java.io.File;
public class DirectoryFileObserver extends FileObserver {
String rootDir;
static final int mask = (FileObserver.CREATE | FileObserver.MODIFY | FileObserver.DELETE);
public final String DIRECTORY_NAME = "Simplified Intent";
public DirectoryFileObserver(String path) {
super(path, mask);
rootDir = path;
}
#Override
public void onEvent(int event, #Nullable String path) {
switch(event){
case FileObserver.CREATE:
String outputDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
Environment.DIRECTORY_PICTURES +
File.separator + DIRECTORY_NAME;
// do something here
break;
case FileObserver.MODIFY:
// will be implemented later
break;
case FileObserver.DELETE:
// will be implemented later
break;
}
}
}
But the onEvent function never gets executed, as suggested by Debug. What is the reason behind it? I have even changed the default save location of the camera application to Internal Storage (initially it was set to SD card) and ran my app again, but nothing changed. I have mentioned both READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions in the manifest file.

How to record and save a video using Google Glass

I am fairly new to android studio and developing with android. I have been trying this for months now, I need to record and save a video using Google Glass. The code that I currently have follows. This is code inspired from https://developers.google.com/glass/develop/gdk/camera#images
When I run this process, the video will start recording and then when the recording is stopped, it will give me the option "tap to accept" When I tap, nothing happens. I understand this is because my method "processVideoWhenReady" does absolutely nothing.
I would like to know if anyone could help me with what I should include in that method to save the video and allow it to be viewed by the user within my application.
Any literature or websites that have useful information about developing for Google Glass would also be a huge help if possible. Google's documentation doesn't seem very helpful to me and very limited.
import android.app.Activity;
import android.graphics.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.content.Intent;
import android.os.FileObserver;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.VideoView;
import com.google.android.glass.content.Intents;
import com.google.android.glass.widget.CardBuilder;
import java.io.File;
import java.io.IOException;
public class RecordActivity extends Activity {
private static final int CAMERA_VID_REQUEST = 1337;
static final int REQUEST_VIDEO_CAPTURE = 1;
public VideoView mVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_record);
takeVideo();
}
private void takeVideo(){
Intent startVideo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (startVideo.resolveActivity(getPackageManager()) != null){
startActivityForResult(startVideo, REQUEST_VIDEO_CAPTURE);
}
//RecordActivity.this.startActivityForResult(startVideo, CAMERA_VID_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
//System.out.println("Entered onActivityResult() method");
String a = getIntent().getStringExtra("record");
System.out.println(a);
if(requestCode == CAMERA_VID_REQUEST && resultCode == RESULT_OK) {
//System.out.println("Result Okay");
Uri videoUri = data.getData();
mVideoView.setVideoURI(videoUri);
String videoPath = data.getStringExtra(Intents.EXTRA_VIDEO_FILE_PATH);
processVideoWhenReady(videoPath);
}
super.onActivityResult(requestCode, resultCode, data);
}
protected void processVideoWhenReady(final String videoPath){
final File videoFile = new File(videoPath);
if (videoFile.exists()){
//Process Video
}
else {
final File parentDirectory = videoFile.getParentFile();
FileObserver observer = new FileObserver(parentDirectory.getPath(), FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
private boolean isFileWritten;
#Override
public void onEvent(int event, String path) {
if (!isFileWritten) {
//make sure file was created in directory expected
File affectedFile = new File(parentDirectory, path);
isFileWritten = affectedFile.equals(videoFile);
if (isFileWritten) {
stopWatching();
runOnUiThread(new Runnable() {
#Override
public void run() {
processVideoWhenReady(videoPath);
}
});
}
}
}
};
observer.startWatching();
}
}
}

Android ImageView content disapearing after showing

My application does this: takes a photo, then show the photo in an ImageView. The weird thing is that the photo is displayed for about a second (after taking it with the camera), and then the ImageView is empty again.
This is my code:
publish.xml
<ImageView
android:id="#+id/itemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
PublishActivity.java
package ar.com.guiagratis;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class PublishActivity extends Activity {
final int TAKE_PICTURE_REQUEST_CODE = 115;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.publish);
}
public void btnNextClick(View v) {
// TODO: disable all buttons
//Intent intent=new Intent(getApplicationContext(), TakePhotoActivity.class);
// startActivityForResult(intent, TAKE_PICTURE_RESULT_CODE);
Toast.makeText(getApplicationContext(), "Sacate una foto viteh", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PICTURE_REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
imageUri, Toast.LENGTH_LONG).show();
Bitmap myBitmap = BitmapFactory.decodeFile(imageUri.getPath());
BitmapDrawable ob = new BitmapDrawable(myBitmap);
ImageView myImage = (ImageView) findViewById(R.id.itemImage);
myImage.setBackgroundDrawable(ob);
Toast.makeText(getApplicationContext(), "Qué linda foto! ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Hubo un problema al subir la imágen... ", Toast.LENGTH_SHORT).show();
}
}
}
}
Any help will be appreciated.
You're using setBackgroundDrawable(Drawable drawable) which sets the View's background.
If you want to change the ImageView's content you need to use
setImageDrawable(Drawable drawable)
or
setImageBitmap(Bitmap bm)
Ok, I finally found the problem. I was not aware that I need to use onSaveInstanceState and onRestoreInstanceState to store the values I don't want to loose from an Activity when I'm doing certain things, like initiating the Camera and taking a photo.
So I added this code:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("photoUploaded", photoUploaded);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
photoUploaded = savedInstanceState.getBoolean("photoUploaded");
if (photoUploaded) {
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
Bitmap myBitmap = BitmapFactory.decodeFile(imageUri.getPath());
BitmapDrawable ob = new BitmapDrawable(myBitmap);
ImageView myImage = (ImageView) findViewById(R.id.uploadedImage);
myImage.setBackgroundDrawable(ob);
}
}
and now is working.
Not sure how to do to make this question as solved.

Categories

Resources