Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
im doing online application. One of the functions are user can choose photo from their gallery or take photo from their camera.Now i have trouble in onActivityResult method where i failed to merge it.Hope anyone can help me to merge it so that the images that user choose or took can view in my ImageView (image_view). This is my main activity:
**private static final int REQUEST_CODE = 1;
private Button button_1;
public int TAKE_PICTURE = 1;
private ImageView image_view;
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sell);
image_view = (ImageView) findViewById(R.id.resul);
button_1 = (Button) findViewById(R.id.button4);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
}
});
Button buttonLoadImage = (Button) findViewById(R.id.button3);
buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
image_view.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onActivityResult2(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri targetUri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
image_view.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
enter code here
You do not need create two onActivityResult() method for two startActivityForResult call. Just provide different request code in both startActivityForResult call. and onActivityResult will notify you with given request code to identify the result.
private static final int TAKE_PICTURE = 100;
private static final int CHOOSE_PICTURE = 101;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sell);
image_view = (ImageView) findViewById(R.id.resul);
button_1 = (Button) findViewById(R.id.button4);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// Here provide TAKE_PICTURE request code to identify the IMAGE_CAPTURE result in onActivityResult method.
startActivityForResult(intent, TAKE_PICTURE);
}
});
Button buttonLoadImage = (Button) findViewById(R.id.button3);
buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
// Here provide CHOOSE_PICTURE request code to identify the ACTION_PICK result in onActivityResult method. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, CHOOSE_PICTURE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK)
// Do something with take picker result
}else if (requestCode == CHOOSE_PICTURE && resultCode == Activity.RESULT_OK)
// Do something with choose image result
}
}
Related
public class addBoard extends AppCompatActivity {
private final int GET_GALLERY_IMAGE = 200;
Uri image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addboard);
final EditText EDTITLE = findViewById(R.id.editTitle);
final EditText EDTEXT = findViewById(R.id.editText);
ImageView imgView = (ImageView)findViewById(R.id.imageView);
Button addPhoto = findViewById(R.id.photo);
addPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, GET_GALLERY_IMAGE);
}
});
try {
Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), image);
imgView.setImageBitmap(bm);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Button backButton = findViewById(R.id.backButton);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("Input_Title", EDTITLE.getText().toString());
intent.putExtra("Input_Text", EDTEXT.getText().toString());
intent.putExtra("Input_Image", image);
setResult(RESULT_OK, intent);
finish();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_GALLERY_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri selectImage = data.getData();
image = selectImage;
}
}
}
If you run this code in another project, there is no error, but if you use it in this project, you will get an error. Why is that?
Error :Caused by: java.lang.NullPointerException: uri
at com.example.toolbar.addBoard.onCreate**(addBoard.java:45)** // blue line
An error occurs during the operation to place Uri as bitmap in imageView.
[This is my code. Please give full code.And Received image should be appear after restart the app. Thank You][1]
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageBitmap(bitmap);
String[] filePathColon={MediaStore.Images.Media.DATA};
Cursor cursr=getContentResolver().query(uri, filePathColon, null, null, null);
cursr.moveToFirst();
int columnindex=cursr.getColumnIndex(filePathColon[0]);
final String picturepath=cursr.getString(columnindex);
cursr.close();
b1= (Button) findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent(edit_student.this,student.class);
intent.putExtra("imagePath",picturepath );
startActivity(intent);}});
} catch (IOException e) {
e.printStackTrace();
}
}
send the image Uri to the second activity like this:
#override
public void onClick(View v){
Intent intent = new Intent(getApplicationContext(),student.class);
intent.putParceleableExtra("imageUri",uri);
startActivity(intent);
}
In ur SecondActivity onCreate() get intent and get the uri,create a bitmap object from the uri and set it into the image view.
Uri retreivedUri = getIntent().getParceleableExra("imageUri");
public class Profile extends Fragment implements Profile_frg{
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog d = new Dialog(mainActivity);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.activity_custom_dialog);
d.setCanceledOnTouchOutside(true);
gallery = (ImageView) d.findViewById(R.id.imageView1);
camera = (ImageView) d.findViewById(R.id.imageView2);
cancel = (ImageView) d.findViewById(R.id.imageView3);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.dismiss();
}
});
gallery.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(
gintent, "Select Picture"), PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(mainActivity,
e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
d.dismiss();
}
});
camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// define the file-name to save photo taken by Camera
// activity
String fileName = "new-photo-name.jpg";
// create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image captured by camera");
// imageUri is the current activity attribute, define
// and save it for later usage (also in
// onSaveInstanceState)
imageUri = context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
// create new Intent
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, PICK_Camera_IMAGE);
d.dismiss();
}
});
d.show();
}
});
}// Work Fine till here...
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}//didn't detect this method
Yes, there is no onActivityResult() callback in fragments.
You have to override activityResult method in your host activity(in which your fragment is defined)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == GALLERY/CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Fragment yourFragment = getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG"); // same tag while adding fragment for the first time.
if (yourFragment != null) {
yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment.
}
}
super.onActivityResult(requestCode, resultCode, data);
}
And in your fragment do like this :
public void onActivityResult(int requestCode,int resultCode,Intent data) {
...
Pull your image data from data object
do your further process from here.
...
}
Yes, startActivityForResult will not work directly if you are calling it from any fragment. After getting the result, the callback will hit the onActivityResult of the Hosting Activity from where you have to manually redirect it to the respective fragment.
Below is the sample code of the onActivityResult of your Activity. Please note that this only redirect the result to the respective fragment.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1001:
Fragment frag = getSupportFragmentManager().findFragmentByTag("TAG"); // TAG should be same as the one you entered while adding the fragment
if (frag != null) {
frag .onActivityResult(requestCode, resultCode, data);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
When I take an image and set it to imageview and then restart my app, imageview will be set to blank. What could the issue be?
Here is my code:
private void initialize() {
iv = (ImageView) findViewById(R.id.breturnpic);
ib = (ImageButton) findViewById(R.id.btakepic);
b = (Button) findViewById((R.id.bset));
b.setOnClickListener(this);
ib.setOnClickListener(this);}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bset: {
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
case R.id.btakepic: {
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameradata);
break;}}}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);}}
If you want to use the image picked on previous app sessions you should persist the data somehow. If is just an image you could save it in your device and keep a reference to the image's URI in your app's shared preferences.
You could also use your database to save these URI in a more structured fashion.
I am calling the camera and capturing images, I will have to capture 10 images one by one and store them on SD card before I can set them to the Image view. Please check my below code, it does not set to the image view.
How would I store it on the SD card and retrieve it to set to the image view? How would I name the images before storing?
In the first activity I am calling the camera:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
init();
}
private void init() {
String extStorageDirectory = Environment.getExternalStorageDirectory()
+ "/testing";
File xmlDirectory = new File(extStorageDirectory);
if (!xmlDirectory.exists())
xmlDirectory.mkdirs();
iv1 = (ImageView) findViewById(R.id.iv1);
}
private OnClickListener onBtnClicked = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case PHOTO:
Intent selectImageIntent = new Intent(first.this,
second.class);
startActivityForResult(selectImageIntent, 1);
break;
}
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
Log.d("*****************",
"inside onactivityresult in main activity=" + result);
Bitmap bitmap = BitmapFactory.decodeFile(result);
iv1.setImageBitmap(bitmap);
iv1.setScaleType(ScaleType.FIT_XY);
}
}
}
And in my second activity I am capturing the image and passing it to the first activity:
private void init() {
picturePath = Environment.getExternalStorageDirectory() + "/Camera/"
+ "test.jpg";
System.out.println("thumbnail path~~~~~~" + picturePath);
File file = new File(picturePath);
outputFileUri = Uri.fromFile(file);
}
public void startCamera() {
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result", picturePath);
setResult(RESULT_OK, returnIntent);
finish();
}
}
}
it is not a good Practice to initialize your object inside Other Method.
remove this line iv1 = (ImageView) findViewById(R.id.iv1); from init() method and Change your OnCreate() to Below Way.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Initialize here
iv1 = (ImageView) findViewById(R.id.iv1);
mContext = this;
init();
}
hope it will help you.
use this code & put your file name & path, camera will capture the image & store it with given name
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (!APP_FILE_PATH_Media.exists())
{
APP_FILE_PATH_Media.mkdirs();
}
uriSavedImage =new File(APP_FILE_PATH_Media+ "/" +
"filename"+ ".jpeg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(uriSavedImage));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
in onActivityResult() use this code to set imageView
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
bm = BitmapFactory.decodeFile(uriSavedImage.getAbsolutePath(), options);
}
catch(Exception ee)
{
}