I want the user to be able to select a picture from a given set of pictures that come with my app.
Is it possible to use new Intent(Intent.ACTION_PICK) but not with media files on the phone of the user, but with some pictures/bitmaps I defined earlier? So far I only found examples to pick images from the gallery.
No, sorry, there is no ACTION_PICK that will pick images from your project's assets/ folder. You would need to create your own UI for that.
No.Intent(Intent.ACTION_PICK) choose picture from Gallery in your phone.
private static final int SELECT_PHOTOS = 100;
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTOS);
For asset folder Check this
try
{
// get input stream
InputStream inputStream = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(inputStream, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex)
{
return;
}
Related
I have in my phone two folders Card and Phone. I create folder MyPhone in the folder Card and added in her an image flap.png. This image I want to show in the element ImageView. I want to use this variant. What am I doing wrong? Thank you.
button1.setOnClickListener {
var intent: Intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.parse("/mnt/sdcard/MyPhoto/flap.png"),"image/*")
imageView1.setImageURI(intent.data)
}
if you want only this image you can use below code
File imgFile = new File("/mnt/sdcard/MyPhoto/flap.png");
if(imgFile.exists()){
Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView1.setImageBitmap(bm);
}else{
Log.e("tag","FILE NOT EXIST")
}
I'm creating an image filter app in Android studio. first, the user selects an image from gallery and it will be displayed in imageview. Then the user clicks edit button and that image is displayed in imageview of next activity where we can add filters... It works fine with low resolution images but when I select any high resolution image it is shown in first imageview but when I click edit button either the app crashes or the last image I had selected is displayed.I searched for the solution but couldn't find it. If anyone knows how to solve this problem please help me
There is a limit to the size of data that can be passed through an intent. The limit is roughly 500Kb - your high resolution photographs will be larger than this.
Consider saving your image to a file location on the device, passing the URI to the receiving activity and loading it within there.
first paste crash logs.
then instead of passing image itself just pass image path.
or simply add the edit tools and mainView in one activity and make edit tools invisible! however you can use fragment too.
use with putExtra to send the Uri Path:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setClass(ThisActivity.this, NewActivity.class);
intent .putExtra("KEY", Uri);
startActivity(intent );
You just need to add path of image.
It's better to save the image in storage and pass the Uri of location instead of passing the image.
Save image in storage:-
public static Uri saveImageOnExternalStorage(Bitmap capturedBitmap, String imageId) {
if (null != capturedBitmap ) {
OutputStream fOutputStream;
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path + "temp", mediaId + ".png");
file.delete();
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
if (file.createNewFile()) {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(COMPRESS_FORMAT, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
return Uri.fromFile(file); // return saved image path on external storage
}
} catch (FileNotFoundException e) {
Log.e(TAG,e.getMessage());
return null;
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG,e.getMessage());
}
}
return null;
}
Now the same Uri you can pass in the intent of next activity:-
Intent intent = new Intent(CurrentActivity.this, LaunchActivity.class);
intent .putExtra("image_key", Uri);
startActivity(intent );
I am saving image bytes through my application using this simple method:
public static boolean StoreBytesToFile(byte[] messagebytes, String FilePathName)
{
try
{
FileOutputStream fos = new FileOutputStream(FilePathName);
fos.write(messagebytes);
fos.flush();
fos.close();
}
catch(java.io.FileNotFoundException fnfe)
{
return false;
}
catch(java.io.IOException ioe)
{
return false;
}
return true;
}
And then when user wants to see the stored image, I open it using gallery intent:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + m_ImageFilename);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
However, gallery shows only the single image file pointed by m_ImageFilename despite there are other images stored in the same folder.
Thus actual behaviour of gallery intent is this:
While expected behaviour is this:
As an important note, I must mention that after restarting phone, gallery intent shows all images in folder as expected. Also, the behaviour is same on all versions from Jelly Bean to Marshmallow.
So each time after saving image, user has to restart phone in order to see all images in folder through gallery intent.
Please can someone tell what am I missing here and how can this be fixed?
You need to add saved image to gallery ... This will help you.
I am trying to CROP a photo in android, but my temp-photo file always empty(0kb) although I had picked a photo from my gallery.
This is my code :
File dir = new File(Environment.getExternalStorageDirectory(), "pictures/softtime");//save path
if (!dir.exists()) {
dir.mkdirs();
}
currentImageFile = new File(dir, System.currentTimeMillis()+".jpg");//path+filename
//Create if not exists
if (!currentImageFile.exists()) {
try {
currentImageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Intent intentGet = new Intent("android.intent.action.GET_CONTENT");
intentGet.setType("image/*");
intentGet.putExtra("crop",true);
intentGet.putExtra("scale",true);
intentGet.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentImageFile));
startActivityForResult(intentGet,CROP);
I have run this code at my phone , I found that There is a new file created after I pick a photo ,but the new file is empty (0kb).
what can i do ?
Thank you everyone for help.
There is no requirement for ACTION_GET_CONTENT activities to support some undocumented crop extra. If you want to allow the user to crop an image, use an image cropping library.
I'm creating an app that downloads a lot of pictures from a website and stores them all in the cache folder so that it won't take up too much space on the phone.
Now my problem is that I want the user to be able to click on an image and it will load up the image in the default android picture viewer. I've researched and figured out how to do that no problem, but I'm not 100% sure if this method will work. I can call the Intent no problem and the Pictureviewer opens but it doesn't display the images?
Can anyone let me know if this is possible to do this way? Thanks
Here is the code calling the intent and getting the directory and files...
URL url = null;
try
{
url = new URL(assetsToFullScreen[arg2]);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
File cacheDir = SingleArticle.this.getCacheDir();
String fileName = url.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
File file = new File(cacheDir, fileName);
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file),"image/png");
startActivity(i);*/