It's taking the screenshot but not showing in gallery when I check the device storage there are screenshot files but when I try to click them it says unable to find app to open this file.
I have to share the ui of my application as an image
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
Bitmap bitmap = getScreenShot(rootView);
store(bitmap,"share_image");
}
});
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
public void store(Bitmap bm, String fileName){
dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dirPath);
if(!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
shareImage(file);
} catch (Exception e) {
e.printStackTrace();
}
}
private void shareImage(File file){
Uri uri = Uri.fromFile(file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
try {
startActivity(Intent.createChooser(intent, "Share Screenshot"));
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "No App Available", Toast.LENGTH_SHORT).show();
}
}
You are calling your file share_image. It has no extension, so the system will treat it as a blank, basic file.
As you are using png compression, add ".png" to the file name.
store(bitmap,"share_image.png");
Related
I am currently making an Adroid Studio app for school, and in the sign up form I have an image upload feature. It was working up until recently until I fixed my validation, and now it always returns an error (unrelated to the validation file) instead of properly saving the image on the device.
The code of the camera launcher:
//Inside OnCreate
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!CheckPermission(RegisterActivity.this))
RequestPermission(RegisterActivity.this);
else {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From Camera");
cameraUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
StartCamera.launch(cameraIntent);
}
}
});
//Outside of OnCreate
ActivityResultLauncher<Intent> StartCamera = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == RESULT_OK){
try {
cameraBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), cameraUri);
photo.setImageBitmap(cameraBitmap);
photoError.setError(null);
}catch (IOException e){
e.printStackTrace();
}
}
}
});
The code of the gallery launcher:
//Inside OnCreate
gallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!CheckPermission(RegisterActivity.this))
RequestPermission(RegisterActivity.this);
else
StartGallery.launch("image/*");
}
});
//Outside of OnCreate
ActivityResultLauncher<String> StartGallery = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
#Override
public void onActivityResult(Uri result) {
try{
if(result!=null) {
cameraBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), result);
photo.setImageBitmap(cameraBitmap);
photoError.setError(null);
}
}catch (IOException e){
e.printStackTrace();
}
}
});
The code of the bitmap saving function:
//Outside of OnCreate
public boolean SaveBitmapInFolder(Bitmap bitmap){
photoName = new SimpleDateFormat("yyMMdd-HHmmss").format(new Date()) + ".jpg";
File dir = new File(Environment.getExternalStorageDirectory(), "/MyPhoto");
if(!dir.exists())
dir.mkdirs();
File dest = new File(dir, photoName);
try{
FileOutputStream out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
return true;
}catch (Exception e){
photoError.setError("An unexpected error has occurred.");
e.printStackTrace();
}
return false;
}
I have tried to rebuild the project in the build tab (It solves some issues occasionally) and clean up the code of the launchers and saving functions. In the case that the SaveBitmapInFolder function returns a false value, I have a toast that shows that an error has occurred, and this is always the result, no matter if the picture was uploaded from the gallery or taken by the camera.
Does anyone know a fix to this issue? There aren't any errors in the project logs, it just immediately goes to the catch and returns false.
I've been looking for various tutorials but no one can solve my case. I hope here is something that can make me understand, about how to open PDF file automatically after finished download from database to android.
This is my download script.
#Override
public void onBindViewHolder(HolderData holder, int position) {
final ModelData md = mItems.get(position);
holder.txtname.setText(md.getName());
holder.txtwaktu.setText(md.getWaktu());
//Proses Downloading
holder.relativeLayoutMateri.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
uri = String.valueOf(Uri.parse("http://192.168.43.144/MLearning/crud/"+md.getPath()));
dm = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uri));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
longid = dm.enqueue(request);
Toast.makeText(context, md.getName()+" Berhasil Di Download"+md.getPath(), Toast.LENGTH_SHORT).show();
}
});
}
Here is the solution please use ACTION_VIEW Intent to open all you need the path of downloaded pdf.
private static String filepath = Environment.getExternalStorageDirectory().getPath()+"/myfile.pdf";
File file = new File(filepath);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Log.d(TAG,e.getMessage());
}
}
Wassup Guys, i'm doing a college project, and my app is a meme generator. After the text plotted, i'm trying to save the image pushing the button. The toast appears, but, it takes more than an hour to the pic show on the gallery. How can i make this thing save immediately?
So there are my codes to create the button listener and the store function
save.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
View content = findViewById(R.id.lay);
Bitmap bitmap = getScreenShot(content);
currentImage = "meme" + System.currentTimeMillis() + ".png";
store(bitmap, currentImage);
share.setEnabled(true);
}
});
public void store(Bitmap bm, String fileName){
String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Memes";
File dir = new File(dirPath);
if(!dir.exists()){
dir.mkdir();
}
File file = new File(dirPath, fileName);
try{
FileOutputStream fos = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(this, "SALVOU ARROMBADO", Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(this, "DEU RUIM VACILĂO", Toast.LENGTH_SHORT).show();
}
}
You have to use MediaScannerConnection like below .Create a method refreshMedia() -
public void refreshMedia(){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
MediaScannerConnection.scanFile(ApplicationContext.context, new String[] { imageFile.getPath() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
//gallery refreshed.
Log.i(TAG, "Scanned " + path);
}
});
}
}, 1500); //this is to refresh media after 1.5 second delay
}
Here imageFile.getPath() is path for your image.
Use it as below-
save.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
store(bitmap, currentImage);
//after saving image call refreshmedia()
refreshMedia();
}
After I take a picture I store the picture into ImageView, So if anyone has an idea or suggestion in how to store the picture after it shown on the ImageView into phone stage without user interaction
Thanks in advance
public class MainActivity extends Activity {
ImageView viewpict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpict=(ImageView) findViewById(R.id.pict_result);
Button btn= (Button)findViewById(R.id.camera);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent intent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// Intent intent = new Intent (getApplicationContext(),MainActivity2.class);
//startActivity(intent);
startActivityForResult(intent,0);
}
});
}
protected void onActivityResult( int requestCode, int resultCode,Intent data)
{
if (requestCode==0)
{
Bitmap theimage = (Bitmap) data.getExtras().get("data");
viewpict.setImageBitmap(theimage);
}
}
}
Try:
viewpict.buildDrawingCache();
Bitmap bm=viewpict.getDrawingCache();
And save:
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
And permission in AndroidManifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
file hase created and also display but it store in by default dcim folder
public class MainActivity extends Activity {
Button camera;
ImageView mImageView;
static final int REQUEST_TAKE_PHOTO = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera=(Button)findViewById(R.id.captureButton);
mImageView= (ImageView) findViewById(R.id.imageView);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
}
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
}
why storageDir folder has not created ? how can i create a folder ? and store images in particular folder give me some code which help me thanks in andvace
This worked for me.
File myDir=new File("/sdcard/folder_you_want_to_save_in");
myDir.mkdirs();
String fname = "JPEG_"+ timeStamp +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}