How do I get the resource id of an image if I know its name? [duplicate]
With something like this: String mDrawableName = “myappicon”; int resID = getResources().getIdentifier(mDrawableName , “drawable”, getPackageName());
With something like this: String mDrawableName = “myappicon”; int resID = getResources().getIdentifier(mDrawableName , “drawable”, getPackageName());
This isn’t a direct answer to the question (loading images >2048), but a possible solution for anyone experiencing the error. In my case, the image was smaller than 2048 in both dimensions (1280×727 to be exact) and the issue was specifically experienced on a Galaxy Nexus. The image was in the drawable folder and none … Read more
Use the below code to save the image to internal directory. private String saveToInternalStorage(Bitmap bitmapImage){ ContextWrapper cw = new ContextWrapper(getApplicationContext()); // path to /data/data/yourapp/app_data/imageDir File directory = cw.getDir(“imageDir”, Context.MODE_PRIVATE); // Create imageDir File mypath=new File(directory,”profile.jpg”); FileOutputStream fos = null; try { fos = new FileOutputStream(mypath); // Use the compress method on the BitMap object to … Read more
I too needed a rounded ImageView, I used the below code, you can modify it accordingly: import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; public class RoundedImageView extends ImageView { public RoundedImageView(Context context) { super(context); } public … Read more
Here’s the correct way of doing it: protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Uri imageUri = data.getData(); Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); } } If you need to load very large images, the following code will load it in in tiles (avoiding large memory … Read more
I used to do it with the dennis.sheppard solution: viewToUse.setImageResource(0); it works but it is not documented so it isn’t really clear if it effects something else in the view (you can check the ImageView code if you like, i didn’t). I think the best solution is: viewToUse.setImageResource(android.R.color.transparent); I like this solution the most cause … Read more
Updated answer, nearly 5 years later: The code in the original answer no longer works reliably, as images from various sources sometimes return with a different content URI, i.e. content:// rather than file://. A better solution is to simply use context.getContentResolver().openInputStream(intent.getData()), as that will return an InputStream that you can handle as you choose. For … Read more
This is pretty late in response, but for anyone else that is looking for this, you can do the following code to manually round the corners of your images. http://www.ruibm.com/?p=184 This isn’t my code, but I’ve used it and it’s works wonderfully. I used it as a helper within an ImageHelper class and extended it … Read more