get Asset UriString - android

I look for a solution to replace
Images. Media. EXTERNAL_CONTENT_URI (type String uriString) by my asset
InputStream ims = getAssets().open("a.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
Bitmap bitmap=drawableToBitmap(d);
Uri imageUri = Uri.parse( Images.Media.EXTERNAL_CONTENT_URI + "/" );
bitmap = ImageLoader.loadFromUri( this, imageUri.toString(), 1024, 1024 );
mImageView.setImageBitmapReset( bitmap,0,true);
thank

You can try this:
File file = new File("path_to_image_file");
Uri imageUri = Uri.fromFile(file);

Related

Get image from drawable by path

I am trying to get image that inside drawable folder by path
so i try this but not working
String path = "android.resource:///" + BuildConfig.APPLICATION_ID + "/drawable/logo_ataturk";
File imgFile = new File(path);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageBitmap(myBitmap);
}
I found this simplest solution
Uri path = Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/drawable/logo_ataturk");
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageURI(path);
Resources are not files on the device. They are files on your developer machine, nothing more.
You can replace all of this code with:
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageResource(R.drawable.logo_ataturk);
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier($name_of_the_image, "drawable", getPackageName()));
or
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
imgLogo.setImageBitmap(myBitmap);

Select image from specific folder Android

Hi i want to get image from a specific folder. Now im using this code:
Intent intent = new Intent();
Uri uri = Uri.parse(Environment.DIRECTORY_PICTURES);
intent.setDataAndType(uri, "image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Scegli foto"),PICK_IMAGE_REQUEST);
Now,with that uri i see the recent and i have to open manually folder.I've tryed setting uri.parse input with the path of my folder,but i got anyway my recent.How can i open automatically my folder?
try this my friend
private void getImages() {
String[] filenames = new String[0];
File path = new File(Environment.getExternalStorageDirectory() + "/FolderName");
if (path.exists()) {
filenames = path.list();
}
ArrayList<String> imagesPathArrayList;
for (int i = 0; i < filenames.length; i++) {
imagesPathArrayList.add(path.getPath() + "/" + filenames[i]);
Log.e("FAV_Images", imagesPathArrayList.get(i));
///Now set this bitmap on imageview
}
}
set image like this
Bitmap bitmap = BitmapFactory.decodeFile(imagesPathArrayList.get(position));
ImageView.setImageBitmap(bitmap);

Android filepath photo ends up with file:///

I'm trying to get the right filepath to an image I'm taking with the camera. I get the right filepath but it ends up with
file:///storage/emulated/0/Pictures/picture.jpg
when i want it to end up as
storage/emulated/0/Pictures/picture.jpg Any clues on how ti fix this?
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File pictureDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), pictureName);
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pictureDirectory));
imageToUploadUri = Uri.fromFile(pictureDirectory);
//this ends up as file:///
filepath = imageToUploadUri.toString();
Try:
filepath = imageToUploadUri.getPath();
or
File f = new File(imageToUploadUri.getPath());
filepath = f.getAbsolutePath();

Get uri of picture taken by camera

When the user takes a picture with the camera, I want the image displayed in an ImageView and I want to save the Uri to an online database. I heard saving the Uri of a picture is better than saving the path.
So this is how I start the camera:
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getFile(getActivity())));
String a = String.valueOf(Uri.fromFile(getFile(getActivity())));
intent.putExtra("photo_uri", a);
startActivityForResult(intent, PICK_FROM_CAMERA);
where
private File getFile(Context context){
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/Myapp");
if (!directory.exists()) {
directory.mkdirs();
}
String filename = "bl" + System.currentTimeMillis() + ".jpg";
File newFile = new File(directory, filename);
return newFile;
}
In onActivityResult:
Bundle extras = data.getExtras();
String photo_uri = extras.getString("photo_uri"); //null
This is always null. Btw before sending the intent the Uri looks like file://... instead of content:// which is the uri when I open an image from the gallery. I don't know if that's a problem.
Also, should I save the path instead of the Uri to the database? I read that the Uri can be complicated in certain phones or Android versions. When I let the user select an image from the gallery, I save the Uri to the database, so I think the best way is saving the Uri in this case as well.
I tried out many variations, but I get null every time I try to pass a variable with the intent...
After starting the intent:
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentPicture,PICK_FROM_CAMERA);
In onActivityResult:
case PICK_FROM_CAMERA:
Uri selectedImageUri = data.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImageUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
break;
That's all.

Android: BitmapFactory.decodeFile / OpenCV does return invaild Bitmap

I'm taking a picture with the camera activity and then try to read it from the saved location. Unfortunately the Bitmap returnd by:
Bitmap bimage = BitmapFactory.decodeFile( path );
has height and width = -1.
I'm using OpenCV and when I read the image with
Mat img = Highgui.imread(path);
I get a proper matrix. Altough I cannot convert it to a bitmap. When doing
bmp = Bitmap.createBitmap(img.cols(), img.rows(), Config.ARGB_8888);
Utils.matToBitmap(img, bmp);
I get the same result again: A Bitmap with width and height = -1. Do I need to watch out for the image format? Can I set that on the camera intent? Shouldn't
BitmapFactory.decodeFile( path );
automatically realize what the format is?
I'm running this on a Samsung galaxy S with Android 2.3.1 and OpenCV 2.3.1
Thanks for your help.
Samsung has some issues with camera intent Photo capture Intent causes NullPointerException on Samsung phones only
Try this way
// to call camera
String _path = Environment.getExternalStorageDirectory()
+ File.separator + "TakenFromCamera.png";
File file = new File(_path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 1212);
// in activity onResult
if (requestCode == 1212) {
String _path = Environment.getExternalStorageDirectory()
+ File.separator + "TakenFromCamera.png";
mBitmap = BitmapFactory.decodeFile(_path);
if (mBitmap == null) {
} else {
Intent intent = new Intent(AYGCamActivity.this,
myGalleryImage.class);
startActivity(intent);
}
}

Categories

Resources