Passing ImageView from one activity to another activity - android

I have been trying to create an android app with the integration of Adobe creative SDJ but I have encountered some problems.
I have created an Activity where one launches camera or gallery to select an image for editing it. The camera launches and the photo is captured and saved in this path:
storage/emulated/0/Pictures/myAppName/myImage.jpg
It is also supposed to be displayed in an ImageView in the same activity but it isn't being displayed. In another activity, I have integrated the ImageEditing UI of the creative sdk from Adobe which needs an imageUri as an input image.
Here are my codes for displaying Image in the ImageView:
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// camera codes
String path = "storage/emulated/0/Pictures/Touch/touch.jpg";
resultImageView.setImageDrawable(Drawable.createFromPath(path));
// gallery codes
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri = data.getData();
resultImageView.setImageURI(imageUri);
}
}
How can I convert the ImageView into Uri and send it to the next activity? Thanks in advance!

I think you are doing wrong for camera code. as you say your gallery code is working fine .try this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 0) {
//taking clicked image from Intent of activity
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");//storing image
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//adding clicked image to ImageView
imgClicked.setImageBitmap(thumbnail);
Hope this will help you..:-)

Related

Saving a picture at a certain storage location and storing its path into a SQLite Database Table

I'm working on an app with SQLite and I want to be able to select a profile picture for an user (that will be saved locally). Basically I have a crop activity and I want to select a picture, crop it and compress it, then I want to save it in the phone at a certain storage path that will not be deleted by cleaning the cache and will also not be visible in the Gallery of the phone (important). In the mean time, I want the new path to be saved in my SQLite database so it can be used to rapidly load the image in an ImageView where it's necessary.
This is the code where I load, crop, compress and also preview the picture in a test ImageView.
public void chooseProfilePicture(){
CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).setAspectRatio(1,1).start(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
{
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK)
{
assert result != null;
profileImageURI = result.getUri();
File thumbnailURI = new File(Objects.requireNonNull(profileImageURI.getPath()));
try
{
compressedImageFile = new Compressor(activity_user_data.this)
.setMaxHeight(400)
.setMaxWidth(400)
.setQuality(90)
.compressToBitmap(thumbnailURI);
}
catch (IOException e)
{
e.printStackTrace();
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
profilePicker.setImageBitmap(compressedImageFile);
isImageSelected = true;
}
else
if(resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
{
assert result != null;
Exception error = result.getError();
}
}
}

when i choose photo from gallery or take a picture from camera my photo is rotating?

im choosing a photo from gallery or taking a picture from camera and when it sets to an ImageView it's getting rotate , how can i fix it to become without rotate ?
public void setNewImage() {
new android.app.AlertDialog.Builder(getActivity())
.setPositiveButton("camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult(takePicture, 0);
}
})
.setNegativeButton("gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(pickPhoto, 1);
}
})
.show();
}
and here im setting the image to imageview :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
NewpostFragment.post_image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
/* Uri picUri = data.getData();
filePath = getPath(picUri);
img.setImageURI(picUri);*/
}
break;
case 1:
if (resultCode == RESULT_OK && data != null && data.getData() != null) {
/*Uri picUri = data.getData();
filePath = getPath(picUri);
img.setImageURI(picUri);*/
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
NewpostFragment.post_image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
First, ACTION_IMAGE_CAPTURE will not give you a Uri via getData() in onActivityResult(). While some buggy camera apps may do that, most will not. Your choices are:
Provide EXTRA_OUTPUT on the ACTION_IMAGE_CAPTURE Intent, in which case the photo should be stored in the location identified by the Uri that you put into EXTRA_OUTPUT, or
Do not provide EXTRA_OUTPUT, and use getParcelableExtra("data") to get a thumbnail from the camera app
See this sample app for using ACTION_IMAGE_CAPTURE with EXTRA_OUTPUT.
In terms of orientation, if you go down the EXTRA_OUTPUT path, you can use android.support.media.ExifInterface to find out the orientation of the photo, then do something to rotate the image to match (e.g., rotate the ImageView).
See this sample app for using ExifInterface (though I am using a different implementation than android.support.media.ExifInterface).

Cannot resolve symbol 'context'

I'm trying to write an Android app which automatically uploads a picture to a server, but I am stuck on just one line of code:
File f = File(context.getCacheDir(), "filename");
The error I get is
This puzzles me because I see so many examples on the web of people using context.getCacheDir() just fine, whereas I get the error message.
It's probably something wrong with my IDE settings. I am using IntelliJ IDE.
Here's is the context of the context problem:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(context.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
You need to do some basic Java programming tutorials. Java is totally different to JavaScript.
Here, you use context as a variable but you have neither declared it, or initialised it, hence the error.
You could define it (and initialise at the same time)
Context context = this;
since this refers to the current object instance of a class and Activity is a Context, or more precisely, it extends Context.
Alternatively, you could just use this.
File f = File(UploadToServer.this.getCacheDir(), "filename");
The error is bacuse you havent declared context, neither it has been passed as a parameter
change context.getCacheDir() to getApplicationContext.getCacheDir() or this.getCacheDir()
so
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(context.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
will become
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(getApplicationContext.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Add this :
Context context = this;
You need this to initialize context
Then Alt+Enter
You must assign context to this Activity

Android - Cannot set gallery image to ImageView

I've looked through tons of posts and cannot figure out why I can't get this to work. All I want to do is have the user click a button that opens up the gallery app. Then the user selects a picture which automatically closes out the gallery and goes back to my application where it automatically sets that image to an ImageView.
So far, I have it working all the way up until it goes back to my application. It seems to all be fine but the image never shows up in the ImageView.
Here is the XML code for the ImageView:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_gravity="center_horizontal" />
At the beginning of my activity I set the ImageView with this:
ImageView targetImage;
And here is the rest of my code to get the image and set it to my ImageView. There is a button that launches "setGunImage".
public void setGunImage(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
targetImage = (ImageView)findViewById(R.id.imageView1);
Uri selectedImageUri = data.getData();
targetImage.setImageURI(selectedImageUri);
}
}
}
I have tested it on both the simulator with the sd card enabled and an image loaded into and also on a real device. Both give the same behavior. It goes through the gallery steps fine but when it goes back to my application there is no image loaded in the ImageView.
I tried changing the data to a bitmap and setting that but it never showed up either. I know it's probably something super simple that I'm just not seeing so hopefully a fresh pair of eyes can point me in the right direction. Thanks.
I think Imran solution should work fine .............. and you can also try this way
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if( resultCode==RESULT_OK)
{
if(requestCode==SELECT_PICTURE)
{
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
targetImage = (ImageView)findViewById(R.id.imageView1);
targetImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
from link
you are passing URI in setImageURI so fist get path of image using MediaStore.Images.Media.DATA and URI then pass path of image in setImageURI.
try this way:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( resultCode==RESULT_OK)
{
if(requestCode==SELECT_PICTURE)
{
targetImage = (ImageView)findViewById(R.id.imageView1);
Uri selectedImageUri = data.getData();
String selectedImagePath=getPath(selectedImageUri);
targetImage.setImageURI(selectedImageUri);
}
}
}
private String getPath(Uri uri)
{
String[] projection={MediaStore.Images.Media.DATA};
Cursor cursor=managedQuery(uri,projection,null,null,null);
int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Intercepting onActivityResult() for Camera application on Android

In my application, I'm trying to make it so the User can press a button, which will allow them to take a picture using the stock camera application on their phone.
I am following the guide to using an external Camera app to capture images that I can use in my own app from the Android Developers Guide (http://developer.android.com/guide/topics/media/camera.html#intent-receive)
I'm having trouble with the onActivityResult() method, it apparently takes in 3 parameters
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
Log.w("borre","Image saved to:\n" + data.getData());
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
But at the moment, the data Intent is coming back as null, so calling any methods on the Intent parameter throws a NullPointerException
Here's the code I'm using to call up the Camera application (It's basically the same as the code in the guide)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Has anyone had this problem or knows why this Intent is coming back as null?
your are getting data part null bez you are not setting intent.setDataAndType() when you are starting Acitivty.like
public static final String IMAGE_UNSPECIFIED = "image/*";
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
startActivityForResult(intent, 3);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0)
return;
if (requestCode == 2) {
Uri uri=data.getData(); //YOU GET DATA HERE
}
//OR
if (requestCode == 3) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)????
imageView.setImageBitmap(photo);
}
}
or in your case getting image path use:
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
//pic path
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
}

Categories

Resources