I have written a bit of code that when the listview is clicked, the image at that location is stored to external memory, then the file path string is sent with the intent to view the image in the default gallery. The only problem is that it takes a seriously long amount of time (I'm talking 10+ seconds on my thunderbolt).
What I haved tried:
1. Storing the bitmap on internal memory
2. Lowering the quality of the bitmap
Here is the code:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position>0){
Bitmap bmp =adapter.getBitmap(adapter.getData(position-1));
if(bmp!=null){
//String path = context.getCacheDir().getAbsolutePath() + "/view.png";
//File f = new File(context.getCacheDir().getAbsolutePath(),"MemeCache");
//if(!f.exists())
// f.mkdirs();
String path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/view.png";
Toast.makeText(context, "opening in gallery", Toast.LENGTH_SHORT).show();
File file = new File(path);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bmp.compress(CompressFormat.PNG, 100, fos );
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "image/png");
//intent.setDataAndType(Uri.fromFile(f), "image/png");
activity.startActivity(intent);
}else{
Toast.makeText(context, arg0.getItemAtPosition(position).toString() +"is HaAAACkSS!!!!", Toast.LENGTH_SHORT).show();
}
}
}
});
Some things that might help:
1) add some timers or instrumentation to you code to see exactly where you're spending all the time: saving the bitmap to the sdcard, starting the intent, or something else entirely.
2) Once you've added timers and can measure the performance, you can see if shrinking the image or storing the image in a different place helps and if so how much. On some devices, internal storage is on the sdcard itself.
3) depending on your program, you might want to consider pre-saving the files to the sdcard (possibly in the background) so that they are probably already saved by the time the user tries to view them in the gallery.
Related
I have problem with my android code for taking screenshot while on video calling
I found several method for take screenshot on specific layout and this is what I used :
private void takeScreenshot() {
final FrameLayout container = (FrameLayout) findViewById(R.id.remote_video_view_container);
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
container.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(container.getDrawingCache());
container.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
if(mPath!=null){
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
}
else{
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
i try this code for save my draw painting application that will save the drawing layout and then i try to use the same method for save video call layout but return black screen image.
i found that this method cannot use for real-time when i want to record it in video
source : How to programmatically take a screenshot in Android?
so, anyone help me what the best way to solve my problem ?
I'm trying to tap an image view, and open that image inside the default photo viewer:
void handleOpenImage(){
try {
File temp = File.createTempFile("myImage", ".png");
BitmapDrawable bitmapDrawable = (BitmapDrawable) attachedImageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
FileOutputStream stream = new FileOutputStream(temp);
boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
if(!success){
throw new Exception();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(temp), "image/*");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
However, when I call this function, a gallery activity starts, but doesn't display my image. The image file is created successfully at the temp path, double checked that. Why isn't the intent working?
As I asked in the comments, the absolute path of the temp file was /data/data/MY_APPS_IDENTIFIER/cache/ulouder-1004534880.png.
This path ist in the private space each app has and cannot be accessed by other apps for security reasons.
By saving the temp file to another location, the gallery app can access it and displays the image correctly.
android.webkit.WebView has capturePicture() to capture the entire contents of the current WebView, but how would you do this for only a specific element on the webpage? For example, you might have many images and text elements positioned with CSS in a div.There may be many elements but I would just like to capture the div as it is rendered in the WebView.
The only idea I came up with is to load just that div only as the entire HTML of another WebView and call capturePicture. The other question how to take a Picture object to a format that can be used with the iText library (PNG, JPEG, GIF). Is there an easier way to accomplish this?
(A function of my app is to output PDF documents. )
you can use view.setDrawingCacheEnabled(true); to capture the view as image. and then you have to create a method to do that. and here is my saving method
void Save() {
if (null != view.getDrawable()) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
save = view.getDrawingCache();
final File myDir = new File(folder);
myDir.mkdirs();
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
final FileOutputStream out = new FileOutputStream(file);
save.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplication(), "Image Saved",
Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
Toast.makeText(getApplication(),
"Something Went Wrong check if you have Enough Memory",
Toast.LENGTH_LONG).show();
}
} else {
final Toast tst = Toast.makeText(getApplication(),
"Please Select An Image First", Toast.LENGTH_LONG);
tst.setGravity(Gravity.CENTER, 0, 0);
tst.show();
}
view.setDrawingCacheEnabled(false);
}
The idea of my app is to capture image from camera then crop specified area from it.
The problem :
When i save the cropped image in my sd card for the first time to launch the app, it saved properly. but when run my app one more time and take image then crop it. when save it the first image that take and crop at first time appear in the sd card not the current one.
This is my code for save images:
public static void save(Activity activity, Bitmap bm, String name) {
OutputStream outStream = null;
File externalFilesDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File outFile = new File(externalFilesDir, "IDOCR" + File.separator + "Numbers");
if (!outFile.exists())
outFile.mkdirs();
File number = new File(outFile, name + ".PNG");
//if (number.exists())
// number.delete();
try {
//outStream = new FileOutputStream(new File(path));
outStream = new FileOutputStream(number);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
bm.recycle();
System.gc();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Maybe if you are trying to overwrite the previous version of the file, you should first delete the previous one...
You can add:
if (!outFile.exists())
outFile.mkdirs();
else {
outFile.delete();
outFile.createNewFile();
}
Hi I have a gallery in my app which shows the image in an image view when you click on the thumbnail. I am setting up a Long Click Listener for an alert dialog that has 2 buttons one to set as wallpaper and one to share. I have the share intent and the get drawing cache somewhat working. It works in the emulator but not on my phone. I have used many of the examples on this site to get this going but its not working at all. it keeps force closing the app on my phone. any help is appreciated.
alertDialog.setButton2("Share",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
imgView.setDrawingCacheEnabled(true);
Bitmap b = imgView.getDrawingCache();
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 95,fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Save Example", "Image saved.");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/image.jpg"));
startActivity(Intent.createChooser(intent, "Share Picture Via.."));
}
});
alertDialog.show();
return true;
Update: it seems to be a problem with
b.compress(CompressFormat.JPEG, 95, fos);
it keeps saying null pointer.
Turns out the null pointer is actually a read_only error..So its not actually writing the file i get a ioexception read-only file system. Why is it doing this?
imgView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imgView.layout(0, 0, imgView.getMeasuredWidth(), imgView.getMeasuredHeight());
See if this helps before
Bitmap b = imgView.getDrawingCache();
ALso see if this helps
Android drawing cache