Start Activity for result not working - java

I'm using start Activity result for starting a new activity to select a image from gallery and it will return a image path to my main activity to , so that it will insert image in my main activity,
here is my code
Intent intent = new Intent(getApplicationContext(), Image.class);
intent.putExtra(UUID, image.getUuid().toString());
startActivityForResult(intent, PICK_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_IMAGE:
if (resultCode != RESULT_OK)
return;
String uuidStr = data.getStringExtra(UUID);
log.v("image url",uuiStr);
break;
}
}
but i'm getting following crashes
java.lang.RuntimeException: Unable to resume activity
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2,
result=-1,data=Intent { (has extras) }} to activity
{com.write.example/com.write.example.MainWriterActivity}: java.lang.NullPointerException

The intent is wrong, try this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");

Start Activity
private static final int PICK_IMAGE = 1;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
Take Result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == PICK_IMAGE && data != null && data.getData() != null)
{
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] {
android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the image
final String imageFilePath = cursor.getString(0);
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
}

Related

I want to select multiple images from gallery but after selecting images when I click on open its crash

I want to select multiple images from gallery but after selecting images when I click on open the app crashes.
My code:
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
Toast.makeText(this, "images pick"+imageUri, Toast.LENGTH_SHORT).show();
TextView txtview = findViewById(R.id.imguri);
txtview.setText(String.valueOf(imageUri));
Intent intent = new Intent(MainActivity.this,ConvertActivity.class);
intent.putExtra("ImageUri",imageUri.toString());
startActivity(intent);
// image_view.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(MainActivity.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
///pick gallery code
public void pickGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
your Intent
Intent chooserIntent = new Intent();
chooserIntent.setType("image/*");
chooserIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
chooserIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(chooserIntent, "Select
Picture"), 100);
Activity Result
override fun onActivityResult(requestCode: Int, resultCode: Int, data:
Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 100 && resultCode == RESULT_OK && data!=null) {
val clipdata = data.clipData
if(clipdata!=null){
for (i in 0 until data?.clipData!!.itemCount) {
val uri = data?.clipData?.getItemAt(i)?.uri
songlist.add(uri)}
}else{
val uri = data?.data
songlist.add(uri)
}
}

Show selected image in another activity

I've searched on stackoverflow but never found a solution
Here is my situation:
I have a button which start an intent to pick an image
How can I resize proportionally (to keep the aspect ration) and display it on another activity (in imageview)?
I also want to save this resized image to user storage. How can I do that?
Thanks!
Here is my code
MainActivity.java
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, getString(R.string.completeaction)),
PICK_FROM_FILE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap selectedphoto = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestExternal && 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]);
String filePath = cursor.getString(columnIndex);
selectedphoto = BitmapFactory.decodeFile(filePath);
cursor.close();
Intent intent = new Intent(MainActivity.this,PhotoResized.class);
intent.putExtra("data", selectedphoto);
startActivity(intent);
}
}
PhotoResized.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photoresized);
img = (ImageView) findViewById(R.id.imageView1);
}
For resizing the image use sampling technique provided by dev.android here
Use Lrucache for storing images. Use technique shown here

Select a image from the gallery and show it in another Activity

I am making an android application in which i have to select image from gallery by clicking a button and then display it in another activity with two text fields, the problem is i am able to open the gallery and select image from it but i am not able to display image in another activity...
here is my code...
PictureOptions.java
public void buttonGalleryOpen(View view)
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap selectedphoto = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && 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]);
String filePath = cursor.getString(columnIndex);
selectedphoto = BitmapFactory.decodeFile(filePath);
cursor.close();
Intent intent = new Intent(PictureOptions.this,ShowImage.class);
intent.putExtra("data", selectedphoto);
startActivity(intent);
}
PictureOptions.xml
<Button
android:id="#+id/buttonGalleryOpen"
android:layout_width="fill_parent"
android:layout_height="66dp"
android:layout_weight="0.34"
android:onClick="buttonGalleryOpen"
android:text="#string/button_gallery_open" />
ShowImage.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_image);
ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
Bitmap selectedphoto =(Bitmap)this.getIntent().getParcelableExtra("data");
imageview.setImageBitmap(selectedphoto);
}
ShowImage.xml
<ImageView
android:id="#+id/ImageShow"
android:layout_width="200dp"
android:layout_height="200dp" />
All things are working fine and second activity(ShowImage) is also opening except that no iamge is displying....dont know why..?HELP
This line in your code does not make sense:
intent.putExtra("data", "selectedphoto");
You are adding here string "selectedphoto" which is in no way connected to selectedphoto variable you initialised earlier. You could put your bitmap to intent extra as byte array but this is inefficient, especially when the image is large.
Instead of passing bitmap to ShowImage activity, pass your URI and then retrieve actual bitmap in ShowImage activity exactly as you do now in your PictureOptions activity.
intent.setData( uri );
In your ShowImage activity do:
URI imageUri = getIntent().getData();
Yo have a typo in intent.putExtra("data", "selectedphoto");, you are passing a String not the bitmap. Change it in
Intent intent = new Intent(PictureOptions.this,ShowImage.class);
intent.putExtra("data", selectedphoto);
startActivity(intent);
removing the double quote from selectedphoto
private void selectImage() {
final CharSequence[] items = { "Photo Library", "Camera", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select");
Utils.hideSoftKeyboard(getActivity());
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Camera")) {
// camera intent
} else if (items[item].equals("Photo Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, getActivity());
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(tempPath, btmapOptions);
resized = Bitmap.createScaledBitmap(bitmap,
(int) (bitmap.getWidth() * 0.8),
(int) (bitmap.getHeight() * 0.8), true);
profileEditImageView.setImageBitmap(resized);
}
}
}
public String getPath(Uri uri, Activity activity) {
String[] projection = { MediaColumns.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = activity
.managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Intent data is null in onActivityResult when I pick an Image from gallery

I need to get an Image from the gallery, and transform it into a bitmap.
This is the code for sending the Intent :
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == mResultLoadImage && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath,
null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor
.getColumnIndex(filePath[0]));
bitmap = BitmapFactory.decodeFile(imagePath);
someFunction(bitmap);
cursor.close();
}
}
I get always a NullPointerException, and with the debuger, I found out that the data Intent is null.
Any solutions?
Try this, it should work:
Intent intent = new Intent(Intent.ACTION_PICK);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE) {
Uri imageUri = data.getData();
Bitmap imgBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, null);
// then do whatever you want with the bitmap
}
}

Android: Open fileBrowser to get file path

I have been trying to figure out a way for my application to open a file browser where the user can select a video File and have my app store that path into a string to use later. Is there an Intent I can use or function? Any suggestions?
You can do it via intent chooser. Try this code i think this solution solve your purpose.
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Set your required file type
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "DEMO"),1001);
then onActivityResult method.
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
// super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1001) {
Uri currFileURI = data.getData();
String path=currFileURI.getPath();
}}
Here is a code I have used one of my previous projects.
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video "), 33);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 33) {
Uri selectedMediaUri = data.getData();
filemanagerstring = selectedMediaUri.getPath();
selectedMediaPath = getPath(selectedMediaUri);
if (!selectedMediaPath.equals("")) {
filePath = selectedMediaPath;
} else if (!filemanagerstring.equals("")) {
filePath = filemanagerstring;
}
int lastIndex = filePath.lastIndexOf("/");
fileName = filePath.substring(lastIndex+1);
//filepath is your file's path
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return "";
}
use below code.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
and on activity result you get file path.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==RESULT_OK){
String FilePath = data.getData().getPath();
textFile.setText(FilePath);
}
break;
}
}

Categories