Null Pointer Exception on Bitmap bitmap = content.getDrawingCache(); - java

I'm trying to create an app which allows users to screenshot the current view. I'm using the following code to do so.
View content = ((Activity)ctx).findViewById(R.id.rootlayout);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
However, i'm getting a Null Pointer Exception on the following line:
Bitmap bitmap = content.getDrawingCache();
What would this mean, that the view is empty?
Any help would be great!

You need to call content.setDrawingCacheEnabled(true); before you can use that. Then call content.setDrawingCacheEnabled(false); when you're done.

Related

I want to convert my File into a bitmap but it's returning null. I have tried many other ways but still stuck into it

This is my code inside onActivityResult. PathUtil class is getting full file path from Uri. I am picking single picture from Gallery :)
try {
selectedFilePath = PathUtil.getPath(SmoothPhotoActivity.this, selectedImageUri);
File sd = Environment.getExternalStorageDirectory();
File image = new File(selectedFilePath);
String filePath = image.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
Bitmap outputImage = imageFilter.processFilter(bitmap);
ivPhotoSelected.setImageBitmap(outputImage);
} catch (URISyntaxException e) {
e.printStackTrace();
}

How to save bitmap to particular internal storage location in android app

The following is my code. I'm trying to get a bitmap I create into the files folder so I can display it in a gallery. Any clue as a better way of/location to doing this or where my program is running into problems?
try{
BitmapDrawable drawable = (BitmapDrawable) mimageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File directory = new File("data/data/com.example.feedme/files");
if (!directory.exists()){
directory.mkdirs();
}
FileOutputStream fileOutputStream = Record.this.openFileOutput(directory.toString(), Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
} catch (Exception e){
Log.w("BitMapError", "Bitmap was not saved right");
}

Not getting correct Path when Select Image from Gallery

When I take an image by Camera, its works fine and I get the image path to save in SQLite database and can retrieve the path to show required image. But when I select an image from Gallery I don't get the correct path which I need to save in database for the further retrieve. I don't get image cause the path is incorrect when I save path in the database. So my question is - how can I get the correct absolute path of the gallery image?
#RequiresApi(api = Build.VERSION_CODES.N)
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeFile(destination.getAbsolutePath());
ivImage.setImageBitmap(bm);
showImagepathET.setText(destination.getAbsolutePath());// This path will save in database
}
#SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
showImagepathET.setText(destination.getAbsolutePath());// This path will save in database
ivImage.setImageBitmap(bm);
}
Here, I get the correct image to show in ImageView ivImage in both take an image by camera or gallery. But I don't get the path correctly when I pick an image from Gallery(works fine when I take camera Image) which I save in database for further path retrieve.
how i can get this selected image path
For what feels like the 3,735th time, there is no "selected image path". You are getting a Uri back (data.getData()); a Uri does not have to point to a file. For example, http://stackoverflow.com/questions/44057912/not-getting-correct-path-when-select-image-from-gallery is a Uri; it does not represent a file on the filesystem.
If you want a file, use a file-picker library.
At last, I decided to do the same procedure what I did in Method "onCaptureImageResult(Intent data)". The image I get from the Gallery stored again to a directory.I know this is absolutely not the good procedure, but it solved my problem temporally until I get a good answer.
#SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bm1 = BitmapFactory.decodeFile(destination.getAbsolutePath());
ivImage.setImageBitmap(bm1);
String selectedImageUri = data.getData().getPath();
showImagepathET.setText(destination.getAbsolutePath());
}

How to copy image in Clipboard from drawable folder in android?

How to copy image in Clipboard from drawable folder in android ?
I mean copy image from drawable folder in Clipboard and paste anywhere.
Plese Help me.....
if you want to copy the drawable image to sdcard you the below code.
You can do something like this,
if (isSdPresent()) { // to check is sdcard mounted
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bbicon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
String extStorageDirectory = Environment.getExternalStorageDirectory()+ File.separator + "FolderName";
File wallpaperDirectory = new File(extStorageDirectory);
wallpaperDirectory.mkdirs();
OutputStream outStream = null;
File file = new File(wallpaperDirectory,"icon.png");
//to get resource name getResources().getResourceEntryName(R.drawable.icon);
try {
outStream = new FileOutputStream(file);
bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
}
to check SDCard
public boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
You cannot do it. Simply because Android clipboard doesn't support this link.
The only things that you can keep in clipboard are: text, uri and intent.

Display Image with Universal ImageLoader with Bitmap

I wonder how to display image if I have a Bitmap and don't want to give as a parameter URL to image using Universal ImageLoader library.
Bitmap img = getDefaultBitmap();
ImageLoader.getInstance().displayImage(img); // I need something like this (for example with parameters to display image like width and height)
Firstly,
you have to save bitmap and then u can pass that path to show that bitmap into imageview using imageloader.
//-- Saving file
String filename = "pippo.jpg";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
try {
FileOutputStream out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//-- show bitmap to imageview
imageLoader.displayImage(dest.getAbsolutePath(), imageView);

Categories

Resources