How to Load an image in an activity in android. - android

This code save the image in " MyApp ".but how to load the image in other activity
final EditText txtRegid = (EditText)this.findViewById(R.id.regid);
String RegID = txtRegid.getText().toString();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File mImageFile = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApp",
"PIC"+RegID+".jpg");
String mSelectedImagePath = mImageFile.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImageFile));
startActivityForResult(intent, TAKE_PICTURE);

Well, if you have defined the BitMap variable as a data member of your MyApp class, you can easily fetch it in other activities by using a getter() function to fetch that BitMap image which you are storing in MyApp.
Or, you could pass your image along with Intent, by encoding it into String format.

If you know the absolute path of the file. You can use BitmapFactory.decodeFile(filePath, opts) method to get the Bitmap. Then use ImageView's method setImageBitmap() to show it.

Related

Open a photo that has been passed between activities in Android Studio

I have a custom camera app with two activities. The first activity (MainActivity) allows the user to take a photo with a custom camera. I would like to open this photo in the second activity (DrawActivity) so that the user can eventually draw on it. My MainActivity works great, the camera opens, snaps and saves the image to the phones external storage. I am having trouble with the DrawActivity opening the photo. I am passing what I believe to be the Uri of the image from my MainActivity to my DrawActivivty with the following code:
Intent myIntent = new Intent(MainActivity.this, DrawActivity.class);
myIntent.putExtra("mybitmap",values.toString());
startActivity(myIntent);
Where values are defined (before I create the above intent):
FileOutputStream fout = new FileOutputStream(imageFile);
fout.write(ostream.toByteArray());
fout.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN,
System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, imageFile.getAbsolutePath());
MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
I can not get the DrawActivity to display the photo. I set up a Toast message just to see what my DrawActivity was receiving, and I got this message. I set up the toast code with this:
Bundle extras = getIntent().getExtras();
String imageuri = extras.getString("mybitmap");
Toast.makeText(this, imageuri, Toast.LENGTH_LONG).show();
and try to pass it to my image view with:
ImageView iv = (ImageView) findViewById(R.id.imageDisplay);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(imageuri));
iv.setImageBitmap(bitmap);
iv.setVisibility(View.VISIBLE);
When I pull up the photo properties in the stock photo app on my phone the image path is this. Am I passing the wrong information? Too much information? Am I parsing the passed information incorrectly?
You are passing the values which is not the imageUri, and you can get the bitmap from the image path. So, pass the file path only from your MainActivity to DrawActivity. After passing the path, you can get the Bitmap by the following code.
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
Rob i suggest you this kind of User friendly architecture. Because going to another activity is ok but if you can do everything in the same activity like this is good approach as i know.

Android, parse, storing image file from camera

I am trying to store an image file in Parse.com for use as a profile photo.
I have a function used to capture the image:
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
According to Parse documentation, I need to have the data in byte[] form and then create a ParseFile with it. So, I have this:
byte[] data = fileUri.getBytes();
// Save image to Parse
photoFile = new ParseFile("profile_photo.jpg", data);
I am getting an error: cannot resolve method getBytes. Not entirely sure if I am missing an import, or if I am approaching this the entirely wrong way. How can I convert a captured image file to byte form so I can create a ParseFile?

Send ImageView to another activity

I have an activity with an ImageView and I want to send this ImageView in an other activity, how can I do this?
You can't actually pass the ImageView itself. You can however, pass it's value and re-load that in your other Activity in it's own new ImageView.
You can pass data between Activitys in the intent.
This goes per example like this;
Intent intent = new Intent(this, MyNewActivity.class);
intent.putExtra("EXTRA_IMAGEVIEW_URL", myImageViewData);
startActivity(intent)
Then in the started (MyNewActivity) you can fetch that data again;
String imageview_url = getIntent().getStringExtra("EXTRA_IMAGEVIEW_URL");
Use whatever method is appropriate for your type of data.
edit note:
This solution is assuming that you send a simple pointer to the image, not the image itself. You either send the URL or URI from where you load it, your drawable ID or the image path in your file system. Indeed, do not try to send the whole image itself as a base64, binary or whatever you come up with.
You cannot pass imageviews between activties.
Assuming you need to pass the image from one activity to another.
You can pass bitmap by converting it to bytearray as below
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Pass byte array into intent:-
Intent intent = new Intent(FirstActivity.this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
In NextActivity Get Byte Array from Bundle and Convert into Bitmap Image:-
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
Update : 5-9-2019
Note: It is better to store the image in some place on the disk and then pass only the path to the image to next activity. The above might not work if the image is huge.
you can either subclass imageview and implement serializable interface and pass it with this way or you can pass resource id(int) of imageview to another activity then load that activity's imageview with this resource id or you make imageview static and then at the other acitvity you simply call it via FirstClass.imageView
Passing bitmap by converting it to bytearray works fine with me

How can I edit a photo taken via the camera in my application?

In my application I use the camera to take photos.
I use this code to start the camera Activity:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
File file = new File(directory, timeStamp+".png"); //name
Uri outputFileUri1 = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri1);
startActivityForResult(intent, CAMERA_RESULT);
This code is working, but how can I edit the preview image (crop, rotate,...) before my main Activity gets the data in onActivityResult()?
Or how can I start the photo editor for my image from my application?
You should create a Bitmap object out of your image, then you could manipulate it.
String fooFile = "PATH TO FILE";
Bitmap bmp = BitmapFactory.decodeFile(fooFile);
here is a crop example. for more examples just Google for 'Bitmap manipulation android'

How to pass drawable between activities

How can I pass an image, drawable type between activities?
I try this:
private Drawable imagen;
Bundle bundle = new Bundle();
bundle.putSerializable("imagen", (Serializable) unaReceta.getImagen());
Intent myIntent = new Intent(v.getContext(), Receta.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
But it reports me an execption:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable
1) Passing in intent as extras
In the Activity A you decode your image and send it via intent:
Using this method (extras) image is passed in 162 milliseconds time interval
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);
In Activity B you receive intent with byte array (decoded picture) and apply it as source to ImageView:
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
2) Saving image file and passing its reference to another activity
WHY to do so? - taken from http://groups.google.com/group/android-developers/browse_frm/thread/9309931b3f060284#
"The size limit is: keep it as small as possible. Definitely don't put
a bitmap in there unless it is no larger than an icon (32x32 or
whatever).
In *Activity A* save the file (Internal Storage)
String fileName = "SomeName.png";
try {
FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
fileOutStream.write(b); //b is byte array
//(used if you have your picture downloaded
// from the *Web* or got it from the *devices camera*)
//otherwise this technique is useless
fileOutStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Pass location as String to Activity B
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picname", fileName);
In *Activity B* retrieve the file
Bundle extras = getIntent().getExtras();
String fileName = extras.getString("picname");
Make *drawable* out of the picture
File filePath = getFileStreamPath(fileName);
Drawable d = Drawable.createFromPath(filePath.toString());
Apply it to the ImageView resource
someImageView.setBackgroundDrawable(d);
You can tag each image (in the xml, or programmaticlly) with the image resource name (like "img1.png"),
then retrieve the image name using the getTag();
Then use getResources().getIdentifier(image name,"drawable", .getPackageName()) to get the drawable resource id.
And just pass the resource id through the intent -
intent.putExtra("img 1",resource id);
Lastly the result Activity can create the image from the resource using:
getResources().getDrawable(intent.getIntExtra("img 1",-1));
Drawable objects are not inherently serializable, so they cannot be passed directly in Intent extras. You must find another way to serialize or persist the image data and retrieve it in the new Activity.
For example, if you are working with BitmapDrawable instances, the underlying Bitmap could be written out to a file and read back, or serialized into a byte array (if its small enough) and the byte array could be passed via extras of an Intent.
HTH
Much much much better not to pass (or serialize) Drawables around among Activities. Very likely your are getting the drawable out of a resource. Hence there's a resource ID. Pass that around instead, that's just an int. And re-hydrate the Drawable in the other Activity.
If the Drawable is not coming from a resource, but it's built at runtime in memory ... well let's speak about it. #Devunwired has a nice suggestion in that case.
You can simply use a native buildDrawingCache method:
ImageView imageView = imageLayout.findViewById (R.id.img_thumb);
imageView.buildDrawingCache ();
Bundle extras = new Bundle ();
extras.putParcelable ("image", imageView.getDrawingCache ());
Intent intent = new Intent (this, ImageActivity.class);
intent.putExtras (extras);
startActivity (intent);
then get it at your ImageActivity:
Bundle bundle = getIntent ().getExtras ();
if (bundle != null) {
ImageView imageView = findViewById (R.id.img_full);
Bitmap image = bundle.getParcelable ("image");
imageView.setImageBitmap (image);
}
In case you pull those images from web and load them again and again, caching them would be really good option since you'll decrease network traffic and increase loading speed. I suggest using WebImageView from Droid-Fu. Really good solution, used it on some projects and I'm very satisfied with it.
I don't know if this is the case, but if the reason why you are trying to pass a drawable is because you are using an Imageview, just put the resource id in the imageview's tag, pass the tag as an Integer instead of the drawable in the intent's extra and use the following line in the receiving activity:
imageView.setImageDrawable(getResources().getDrawable(getIntent().getIntExtra("image_id",0)));
Hope it will help someone.

Categories

Resources