My application adds a photo from your gallery to an ImageView just fine, but if I try and change the image (after one has already been selected) my app force closes. Is there a way to achieve this?
here is my code..
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.myimageview);
((Button) findViewById(R.id.taptoadd))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public 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);
}
}
Set the image through code .
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
Related
public class Main2Activity extends AppCompatActivity {
private static int PICK_IMAGE_REQUEST = 1;
ImageView imgView;
static final String TAG = "Main2Activity";
public String[] filePathColon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void loadImagefromGallery(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data) {
Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
int nh = (int) (bitmap.getHeight() * (1024.0 / bitmap.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 1024, nh, true);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
imgView = (ImageView) findViewById(imageView);
imgView.setImageBitmap(scaled);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Main3Activity.class);
intent.putExtra("imageUri", filePath);
startActivity(intent);
}
});
} else {
Toast.makeText(this, "No.", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Oops! Sorry", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
public class Main3Activity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
imageView = (ImageView) findViewById(R.id.imageView2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap ba = this.getIntent().getParcelableExtra("imageUri");
imageView.setImageBitmap(ba);
}
}
What is the problem?
Step #1: Delete these lines:
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
Step #2: Replace intent.putExtra("imageUri", filePath); with intent.setData(uri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Step #3: Replace:
Bitmap ba = this.getIntent().getParcelableExtra("imageUri");
imageView.setImageBitmap(ba);
with:
Uri imageUri = getIntent().getData();
imageView.setImageURI(imageUri);
Eventually, replace of your bitmap work (the Bitmap that you are not using in the first activity, and setImageURI() in the second activity) with an image-loading library, such as Picasso, so that you can move all this data processing off the main application thread.
I want to pick only those images from gallery which are clicked from camera,not present from other sources.
Cursor imagecursor = getApplicationContext().getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID}, null,
null, null);
This query gives me all the images.
I want the path to camera images irrespectice of gallery and camera intent.Is there some generic method which returns the path of captured images path
here is an example hope it help!!
package com.mahesh.gallerytestapp;
public class MainActivity extends Activity {
Uri selectedImageUri;
String selectedPath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.bGallery);
Button bCam= (Button) findViewById(R.id.bCamera);
ImageView preview = findViewById(R.id.preview);
bCam.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 100);
}
});
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openGallery(10);
}
});
}
public void openGallery(int req_code){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(data.getData() != null){
selectedImageUri = data.getData();
}else{
Log.d("selectedPath1 : ","Came here its null !");
Toast.makeText(getApplicationContext(), "failed to get Image!", 500).show();
}
if (requestCode == 100 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
selectedPath = getPath(selectedImageUri);
preview.setImageURI(selectedImageUri);
Log.d("selectedPath1 : " ,selectedPath);
}
if (requestCode == 10)
{
selectedPath = getPath(selectedImageUri);
preview.setImageURI(selectedImageUri);
Log.d("selectedPath1 : " ,selectedPath);
}
}
}
public 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);
}
}
I am working on app for editing photos.
I have a button in first activity and ImageView in second activity. When I click the button it would open gallery and I would be able to select an image. The selected image needs to appear in my ImageView in second activity but it doesn't. For time being i am displaying image in first activity it self but can anyone suggest me how to display that image in next activity.
Below is my code.
public class Camera_Gallery_Option extends Activity {
private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_gallery_option);
Button galleryButton= (Button) findViewById(R.id.button1);
galleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
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) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
imageview.setImageURI(selectedImageUri);
}
}
}
public 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);
}
Pass the selectedImageUri to the second activity and set the uri to image view in the second activity ?
startActivity(new Intent(this, SecondActivity.class).setData(selectedImageUri));
In the SecondActivity:
protected void onCreate(Bundle savedInstanceState) {
...
imageView.setImageURI(getIntent().getData());
}
I have some code that get's a pic from the camera gallery. It works fine when you use a button from an activity to get pic and put pic on that activity but I cant get it to my Imageview when I try this with a button on AlertDialog. Any help?
This code by itself is fine
((Button)dialogView.findViewById(R.id.button3))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"),SELECT_PICTURE);}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);}}}
public 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);}});
But when you add this code to AlertDialog it wont function anymore
lay1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
LayoutInflater myLayout = LayoutInflater.from(context);
final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(dialogView);
final AlertDialog alertDialog = alertDialogBuilder.create();
((Button) dialogView.findViewById(R.id.button3))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);
}
}
}
public 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);
}
});
alertDialog.show();
return true;
}
});
I have tested the code on another activity from a Button and it works fine but when I open alertDialog to do it, I cant get the pic back to my imageview. I can still get to my Gallery and click on a pic but when I try to save it it wont put the pic in the imageView.
Try moving the method onActivityResult outside the click listener alongside your other activity methods, like so
public class MyActivity extends Activity {
// other methods and fields
private ImageView im1;
// initialize im1 in your onCreate() method
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);
}
}
}
}
Whenever I try to click on the browse button , I want to browse my gallery and select a photo from it and display it in a new activity.
Until I displayed the photo on the same activity there was no error , but as soon I change the code to transfer the photo on another intent there is a force close error
Here is my code:
Main screen :
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent1 = new Intent(MainActivity.this,Edit.class);
startActivity(intent1);
}
});
Another screen :
public class Edit extends Activity
{
private String selectedImagePath;
private static final int SELECT_PICTURE = 1;
private ImageView img;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
img = (ImageView)findViewById(R.id.ImageView1);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public 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);
}