Android || How to parse the Image from one activity to another? - android

This is my code. I want to add one more button here which onclick send the image to next activity , but I am not able to configure this. How should I do it?
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}

Initially, keep newly added button disable and in onActivityResult method after getting image string enable button and set onClickListener to button. And pass that image string to next activity using intent extra.
In onActivityResult method,
newButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(),NextActivity.class).putExtra("img",imgDecodableString));
}
});

Best way to do this with large image would be using Serializable, since Intent and Parcelable both have size limit.
Answers to this question might help : Serializing and De-Serializing android.graphics.Bitmap in Java

you can put the data in your first activity like
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and in second activity parse the data like below:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
convert your imagepath to bitmap code snipped is:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

//First Activity
Bitmap b = null;
String bitmapString = getStringFromBitmap(b);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("bitmapString", bitmapString);
//Second Activity
String receivedBitmapString = getIntent().getStringExtra("bitmapString");
Bitmap receivedBitmap = getBitmapFromString(receivedBitmapString);
//Functions
private String getStringFromBitmap(Bitmap bitmapPicture) {
/*
* This functions converts Bitmap picture to a string which can be
* JSONified.
* */
final int COMPRESSION_QUALITY = 100;
String encodedImage;
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
return encodedImage;
}
private Bitmap getBitmapFromString(String jsonString) {
/*
* This Function converts the String back to Bitmap
* */
byte[] decodedString = Base64.decode(stringPicture, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return decodedByte;
}
Source:Link

Related

Intent not working in onActivityResult

I am trying to get image from gallery from my 1st Activity and want the resulted image in the ImageView of second activity.
Here is the Code For 1st Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageVew = (ImageView) findViewById(R.id.imageView);
}
public void useGalleryMethod(View view) {
//this is for picking Image from Gallery or file
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
onActivityResult() in 1st Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
try {
//this is for picking Image from Gallery or file
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
imageVew.setImageBitmap(bitmap);
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
intent.putExtra("Bitmap",bitmap);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
The Code For Second Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
Intent getIntentInfo = getIntent();
if(getIntentInfo != null){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = (Bitmap) getIntentInfo.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);
}else{
return;
}
}
The App is running properly and showing the gallery image in ImageView of 1st Class Only and not going to 2nd Activity using the Intent in onActivityResult method.
Please Let Me know whats wrong with my code? ?
or is there any other way and I am not going in the right direction ?
Use this method which is more convenient that Parcelable you used:
method 1: Your can save that to sd card and retrieve.
OR
method 2:
Convert Bitmap to Byte Array:-
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 with Intent:-
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Get Bitmap from ByteArray received in bundle:-
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);
In onActivityResult() of first activity replace this -
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
try {
//this is for picking Image from Gallery or file
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
imageVew.setImageBitmap(bitmap);
// Send Image URI istead
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
intent.putExtra("imageUri", data.getData().toString());
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
And on Second Activity do something like -
if(getIntentInfo != null){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
Uri.parse(getIntentInfo.getStringExtra("imageUri")));
imageVew.setImageBitmap(bitmap);
}else{
return;
}

OutOfMemoryError android : converting bitmap to string (imposiable?)

i'm so fustrated from this little thing that for some reason just doesn't work out...i don't really know what else to do. the pictures im trying to add are been selected by my gallery..(the last thing i've checked was 1024X768 and 32 color bit)
ImageButton img;
private static final int SELECTED_PICTURE = 1;
String picPathFile;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_channel);
img = (ImageButton) findViewById(R.id.imageButton);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE);
}
});
// defines the button functionality
Button b = (Button) findViewById(R.id.backToMenu);
b.setOnClickListener(new View.OnClickListener() {
#Override
// defines to move to menu activity when button is pressed
public void onClick(View v) {
Intent showImageIntent = new Intent(AddChannelActivity.this, ShowPreviewChannel.class);
Bitmap selected_image = BitmapFactory.decodeFile(picPathFile);
showImageIntent.putExtra("pic_file", picPathFile);
showImageIntent.putExtra("c_name", name);
showImageIntent.putExtra("c_id", "777");
startActivity(showImageIntent);
//newImage.recycle(); // where do i put it??
//newImage = null;
//selected_image.recycle();
//selected_image = null;
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case SELECTED_PICTURE: {
if (requestCode == SELECTED_PICTURE) {
Uri uri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
picPathFile = picturePath;
have_picture = true;
}
break;
}
default: break;
}
}
Intent intent = getIntent();
// the rellvant part from the show preview class
Bundle bundle = intent.getExtras();
if(bundle!= null){
String picturePath = intent.getStringExtra("pic_file");
Bitmap bm = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray(); // HERE~!!!!! out of memory
}
ive tried everything....please help me :/
i saw the baos size is 7193767

upload photo to parse ( android)

I'm a new parse.com user and I want to upload image to it that is chosen by the user.
the user choose the image , then the name of it and then press the upload button
but when I click the button upload the program is crash :(
this is my try
Button btn;
ImageView Pic;
ParseObject shop;
final int PHOTO_SELECTED = 1;
ParseFile file;
ParseFile photoFile;
final Context context = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.openshop);
btn = (Button) findViewById(R.id.button1);
Pic = (ImageView) findViewById(R.id.imageView1);
final int PHOTO_SELECTED = 1;
Pic.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,PHOTO_SELECTED);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_SELECTED && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String picturePath = cursor.getString(columnIndex);
cursor.close();
final Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Pic.setImageBitmap(bitmap);
btn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("NewApi")
public void onClick(View arg0) {
EditText name = (EditText) findViewById(R.id.sname);
shop = new ParseObject("imagetest");
// Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Pic);
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile(name.getText().toString()+".png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
//Create
shop.put("name", name.getText().toString());
shop.put("UserOpen",ParseUser.getCurrentUser());
shop.put("Image", file);
shop.saveInBackground();
// Show a simple toast message
Toast.makeText(getApplicationContext(), "Created",
Toast.LENGTH_SHORT).show();
}
});
}
}
}
thankyou

How to specify whether intent is from camera or gallery in another Activity

I'm pretty new with android and I'm having problems specifying whether Intent is coming from camera or gallery in second activity. Here's my code:
MainActivity:
public class MainActivity extends ActionBarActivity implements OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button cameraButton = (Button) this.findViewById(R.id.cameraButton);
Button galleryButton = (Button) this.findViewById(R.id.galleryButton);
//Here we choose to take a new picture
cameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
}
});
// Here we choose a photo from the gallery
galleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case PICK_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
Intent captureIntent = new Intent(MainActivity.this, ImageViewerActivity.class);
captureIntent.putExtra("data", imageBitmap);
startActivity(captureIntent);
}
break;
case PICK_FROM_GALLERY:
if (resultCode == Activity.RESULT_OK)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(this, ImageViewerActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
}
break;
}
}
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
ImageViewerActivity:
public class ImageViewerActivity extends Activity {
public static ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.photo_view);
imageView = (ImageView)findViewById(R.id.imageView);
getData();
}
private void getData() {
// TODO Auto-generated method stub
if(//image is from camera)
{
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("data");
imageView.setImageBitmap(bitmap);
}
if(//image is from gallery)
{
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageView.setImageBitmap(bmp);
}
}
Any help is needed! Thanks a lot!
In your extras, you can pass another field with "INTENT_FROM" and put a String with a identifier = CAMERA, GALLERY or whatever.
To attach it, before sending your intent, create a bundle and add it to the intent that will be sent, like this:
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
You can find tips to manage bundles here.
Passing a Bundle on startActivity()?
I hope this would help you!

Take photo from camera in fragment

In my Fragment I try to take picture from my camera but the onActivityResult of my Fragment is not called. After taking photo this Fragment is not showing and is switching to my first Fragment. In there any other way for capturing photos in a Fragment, or what am I doing wrong?
Here is my current code:
public void takePhoto() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
PhotosListFragment.this.startActivityForResult(intent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 100:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getActivity().getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
viewHolder.imageView.setImageBitmap(bitmap);
Toast.makeText(getActivity(), selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getActivity(), "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
}
Hope this will help you:
public class CameraImage extends Fragment {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1888;
Button button;
ImageView imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.camera_image,
container, false);
button = (Button) rootView.findViewById(R.id.button);
imageView = (ImageView) rootView.findViewById(R.id.imageview);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
return rootView;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// convert byte array to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
imageView.setImageBitmap(bitmap);
}
}
}
}
This is one of the most popular issue. We can found lots of thread regarding this issue. But none of them is useful for ME.
So I have solved this problem using this solution.
Let's first understand why this is happening.
We can call startActivityForResult directly from Fragment but actually mechanic behind are all handled by Activity.
Once you call startActivityForResult from a Fragment, requestCode will be changed to attach Fragment's identity to the code. That will let Activity be able to track back that who send this request once result is received.
Once Activity was navigated back, the result will be sent to Activity's onActivityResult with the modified requestCode which will be decoded to original requestCode + Fragment's identity. After that, Activity will send the Activity Result to that Fragment through onActivityResult. And it's all done.
The problem is:
Activity could send the result to only the Fragment that has been attached directly to Activity but not the nested one. That's the reason why onActivityResult of nested fragment would never been called no matter what.
Solution:
1) Start Camera Intent in your Fragment by below code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Fragment frag = this;
/** Pass your fragment reference **/
frag.startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); // REQUEST_IMAGE_CAPTURE = 12345
2) Now in your Parent Activity override **onActivityResult() :**
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
You have to call this in parent activity to make it work.
3) In your fragment call:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
// Do something with imagePath
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(photo);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri selectedImage = getImageUri(getActivity(), photo);
String realPath=getRealPathFromURI(selectedImage);
selectedImage = Uri.parse(realPath);
}
}
}
4) Reference methods for getting URI:
-> Method for getting Uri from the Bitmap
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
-> Method for getting File path from the Uri
public String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
That's it.
With this solution, it could be applied for any single fragment whether it is nested or not. And yes, it also covers all the case! Moreover, the codes are also nice and clean.
I tried your code its working fine dude. I changed
PhotosListFragment.this.startActivityForResult(intent, 100);
to
getActivity().startActivityForResult(intent, 100);
which after taking the picture, returning back to same activity.
I think both of your fragments are on same activity.
if that is the situation, I suggest you to create a new activity and put the new fragment in there.
For Fragment this is simplest solution:
cameraIamgeView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityFromFragment(PlaceOrderFragment.this, cameraIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
// super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK && data != null) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
/*
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// convert byte array to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
*/
cameraIamgeView.setImageBitmap(bmp);
}
}
}catch(Exception e){
Toast.makeText(this.getActivity(), e+"Something went wrong", Toast.LENGTH_LONG).show();
}
}

Categories

Resources