Why I am unable to set ImageBitmap to RelativeLayout here? - android

I'm trying to select a background image from gallery and set it as Activity background. But the problem is here when I set RelativeLayout instead of ImageView, it gives me error on setImageBitmap, what;s the reason here? Here's my code: I referred to this tutorial : http://viralpatel.net/blogs/pick-image-from-galary-android-app/ Thanks in advance!
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
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 picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.background);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}

That is because RelativeLayout doesn't have a method called setImageBitmap.
Referring to this link, you can use this to set it to your relative layout:
File f = new File(getRealPathFromURI(path));
Drawable d = Drawable.createFromPath(f.getAbsolutePath());
mRelativeLayout.setBackground(d);
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}

You can't do setImageBitmap on a RelativeLayout. I think what you want to do is setBackground which takes in a Drawable as parameter.

Related

How to solve issue of path of any photo in gallery for kitkat and further versions?

I used this onActivityResult method to fetch photo from Gallery or camera
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0) {
finish();
photoFile = null;
theftimage.setImageResource(R.drawable.camera);
}
if (requestCode == REQUEST_TAKE_PHOTO) {
theftimage.setVisibility(View.VISIBLE);
setPic();
}
if (requestCode == SELECT_PICTURE) {
// Get the url from data
if(resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path; //= getPathFromURI(selectedImageUri);
path = ImageFilePath.getPath(getApplicationContext(), selectedImageUri);
String filename=path.substring(path.lastIndexOf("/")+1);
etFileName.setText(filename);
Log.i(TAG, "Image Path : " + path);
// Set the image in ImageView
theftimage.setImageBitmap(BitmapFactory.decodeFile(path));
}
}
}
}
and its method for fetching path
public String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return contentUri.getPath();
}
error is
02-28 11:00:18.488: E/HAL(24576): hw_get_module_by_class: module name gralloc
02-28 11:00:18.488: E/HAL(24576): hw_get_module_by_class: module name gralloc
its giving me error of path in kitkat and further versions. Can you solve this? Help will be appriciated.
You do not need a path if all that you want is putting the selected file in a ImageView.
One statement will do for all Android versions:
theftimage.setImageBitmap(BitmapFactory.decodeStream(
getContentResolver().openInputStream(data.getData())));
Please try the following code:
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
String imgDecodableString;
if(cursor==null) {
imgDecodableString= selectedImage.getPath();
}
else {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
}
}
imgDecodableString will contain the final path of the image and you can set the picture in your ImageView as :
theftimage.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));

How to save a selected Uri pic to drawable?

I am trying to set the picture that the user chooses from their gallery, by using Uri, as their background for an app, but I cant quite figure it out. One thing that I tried doing was straight up setting the background to the uri, but it fails do to compatibility mismatch. How can I do this, either by programmatically setting the drawable or any other way at at all?
Here is what I have tried
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 1) {
if (intent != null && resultCode == RESULT_OK) {
Uri selectedImage = intent.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);
cursor.close();
if (bmp != null && !bmp.isRecycled()) {
bmp = null;
}
bmp = BitmapFactory.decodeFile(filePath);
imageView.setBackground(selectedImage);//error here
//imageView.setBackgroundResource(0);//originally this, but this crashes also
imageView.setImageBitmap(bmp);
}
}
}
Check out this link Retrieve drawable resource from Uri
try {
InputStream inputStream = getContentResolver().openInputStream(yourUri);
yourDrawable = Drawable.createFromStream(inputStream, yourUri.toString() );
imageView.setImageDrawable(yourDrawable);
} catch (FileNotFoundException e) {
yourDrawable = getResources().getDrawable(R.drawable.default_image);
}

changing the pixel color of imageview

i have to change the color of just one pixel of an selected image from gallery
i used a button to change this pixel but always the application has forced to stop when i clicked the button
plz help me to solve this problem :(
this is my button code
public void btnClick2 (View v){
bmp.setPixel(30,30,0xFF000000 );
}
and this is onactivityresult code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECTED_PICTURE && 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 picturePath = cursor.getString(columnIndex);
cursor.close();
bmp = BitmapFactory.decodeFile(picturePath);
iv1.setImageBitmap(bmp);
}
}
it does force close because BitmapFactory.decodeFile returns an immutable Bitmap, while setPixel works only on mutable bitmaps. You can use Bitmap.copy to get a mutable version of the original bitmap.
Edit:
Bitmap tmpBmp = BitmapFactory.decodeFile(picturePath);
bmp = tmpBmp.copy(Bitmap.Config.ARGB_8888 ,true);

Image resolution or size issue gives 'Force Close' error

I have 2 imageView to import images from gallery and set on these imageViews. I basically do this by:
String mPicPath1, mPicPath2;
protected void onCreate(Bundle icicle) {
mPicPath1 = null;
mPicPath2 = null;
}
#Override
protected void onActivityResult(int requestCode, int resultcode, Intent data){
super.onActivityResult(requestCode, resultcode, data);
switch(requestCode){
case 1:
if (data != null && resultcode == RESULT_OK)
{
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]);
mPicPath1 = cursor.getString(columnIndex);
cursor.close();
logoview.setBackgroundResource(0);
logoview.setImageBitmap(BitmapFactory.decodeFile(mPicPath1));
}
break;
case 2:
if (data != null && resultcode == RESULT_OK)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
cursor.moveToFirst();
mPicPath2 = cursor.getString(columnIndex);
cursor.close();
qrcodeview.setBackgroundResource(0);
qrcodeview.setImageBitmap(BitmapFactory.decodeFile(mPicPath2));
}
break;
}
and i use a button onClickListener to start intent and go to SecondActivity:
save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NewCard.this, Template.class);
if (!TextUtils.isEmpty(mPicPath1)) {
intent.putExtra("picture_path1", PicPath1);
}
if (!TextUtils.isEmpty(mPicPath2)) {
intent.putExtra("picture_path2", PicPath2);
}
startActivity(intent);
}
});
And my SecondActivity to set images on 2 different imageViews:
String pre_img_path1= getIntent().getStringExtra("picture_path1");
ImageView crdlogoframe = (ImageView) findViewById(R.id.crdlogoframe);
crdlogoframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path1));
String pre_img_path2= getIntent().getStringExtra("picture_path2");
ImageView crdqrframe = (ImageView) findViewById(R.id.crdqrframe);
crdqrframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path2));
So my problem is about the file size or resolution of the images. If i take 2 high resolution images from gallery (taken by standard camera: 1992kb, 3264x2448) and i click my save (save.onClickListener) button, i receive Force Close error. If i take small size images there is no problem(74kb, 800x600) i can proceed SecondActivity and see images are set. How i can solve this issue. Should i use a syntax to resize the images when i pick or set. The formats are both .Jpeg. Thank you very much.
i do not have the rep to comment yet so .... but as Simon said your images are to large 31MB need to try and keep things under about 16MB so you will have to re-size them before you can display them
Instead of
BitmapFactory.decodeFile(mPicPath2)
You need to use something like
BitmapFactory.decodeFile(mPicPath2, options)
Where options is an instance of BitmapFactory.Options with an inSampleSize set to something like 4 that will scale down the image as it is loaded.

saving image picked from gallery for future use

Hey i have been looking for a while now. the following code picks the image from the android gallery and shows it in an imageView. but heres the thing, everytime the app is closed and restarted the has to be picked again. i would like to know how i can edit the following to save the image for good in the imageView.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
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 picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
The only thing that the user is picking is the path of the picture. So if you save the path to SharedPreferences, then everytime the app is started, you can use your existing code, but just change where you get the path:
String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
EDIT:
This is a complete method you can use in OnCreate:
String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
else {
selectImage();
}
In select image use your current code to start the picking activity, then in onActivityResult use this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
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 picturePath = cursor.getString(columnIndex);
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}

Categories

Resources