I am trying to make wallpaper app but I am facing a problem which is the low resolution after setting the wallpaper.
Although the real picture before setting as wallpaper is very high resolution from url.
This is my code:
public void set(View view) {
Toast.makeText(MainActivity.this, "Setting wallpaper", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(getImageUri(this,bitmaptwo), "image/*");
intent.putExtra("Bitmap", "image/*");
startActivity(intent);
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 0, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
Try and convert the wallpaper to a vector, it will scale without any loss of resolution
check this out regarding vectors https://developer.android.com/studio/write/vector-asset-studio.html
Related
While I select the image from the gallery and shows it in ImageView. The image quality is all right. But, uploading an image on the server, it lost quality and become a blur. I obtain the image from the camera by this code.
private void onCaptureImageResult(Intent data) {
bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, 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();
}
imageView.setImageBitmap(bitmap);
}
Then, I did this work-
private String imageToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
byte[] imgByte=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte,Base64.DEFAULT);
}
and used this function to compress my selected photo. But, it makes the loss of that image quality and image become a blur on the server. Why am I facing this problem?
You could use the .png format as it's lossless and doesn't reduce the image quality. On the other hand the .jpeg format is just the opposite of this.
private String imageToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] imgByte=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte, Base64.DEFAULT);
}
You did not show the intent to start a Camera app.
But you did it in such a way that you only got a thumbnail of the picture taken.
Change the intent. Add an uri where the camera app can save the full picture.
There are 783 examples of such an intent on stackoverflow and even more on the internet.
I want to make a function which share my images using Intent . Problem is; i have png images and when i share images using Intent, it change the format of image form png to jpeg . for example there is no background (transparent) of my image.png when i call intent to share, it changes image background to black and format to image.jpg.
Here is my code
protected void ShareImage( )
{
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image );
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,"title",null);
Uri imageUri=Uri.parse(imgBitmapPath);
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(sharingIntent, "Share images to.."));
}
Please Help me to share image without changing its format .. Thanks
MediaStore.Images.Media.insertImage always writes a JPEG. See its implementation.
You can easily adapt its code to write a PNG instead:
private void shareAsPng(Bitmap bitmap, String title)
{
Long now = System.currentTimeMillis() / 1000;
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, title);
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
values.put(MediaStore.MediaColumns.DATE_ADDED, now);
values.put(MediaStore.MediaColumns.DATE_MODIFIED, now);
ContentResolver cr = getContentResolver();
Uri uri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
try (OutputStream os = cr.openOutputStream(uri)) {
bitmap.compress(Bitmap.CompressFormat.PNG, 0, os);
}
catch (IOException e) {
cr.delete(uri, null, null);
return;
}
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
}
The compression quality parameter is documented to be ignored for PNG.
I intentionally omitted IS_PENDING because that requires Android 10.
Convert bitmap to Uri
private Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.PNG, 99, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
and add your application manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
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.
I want to save my bitmap to cache directory.
I use this code:
try {
File file_d = new File(dir+"screenshot.jpg");
#SuppressWarnings("unused")
boolean deleted = file_d.delete();
} catch (Exception e) {
// TODO: handle exception
}
imagePath = new File(dir+"screenshot.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
it s working fine. But if I want to save different img to same path, something goes wrong. I mean it is saved to same path but I see it old image, but when I click the image I can see the correct image which I saved second time.
Maybe its come from cache but I do not want to see old image because when I want to share that image with whatsapp old image seen , if i send the image it seems correct.
I want to share saved image on whatsapp like this code:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imagePath));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
How can I fix it?
thanks in advance.
Finally I solved my problem like this:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(context,bitmap_));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
v
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "TitleC1", null);
return Uri.parse(path);
}
This is just a guess, but since you save a new image (with the old name, or another, that’s not relevant) you should fire a media scan to that path so that the media provider is updated with new content.
See here:
MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, null);
Or even better, wait for the scan to be completed:
MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, new OnScanCompletedListener() {
#Override
void onScanCompleted(String path, Uri uri) {
// Send your intent now.
}
});
In any case this should be called after the new file is saved. As I said, I have not tested and this is just a random guess.
I'm trying to share an image from the internet using share intents. This doesn't work because I think the image needs to be downloaded first:
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
So I'm downloading an image and then sharing it:
#Background
protected void tryToShareImage(Intent intent) {
try {
URL url = new URL(mImageUrl);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
intent.putExtra(Intent.EXTRA_STREAM, getImageUri(mActivity, image));
} catch (Exception e) {
e.printStackTrace();
}
startActivity(Intent.createChooser(intent, "Share using..."));
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
However, there's one problem: the image stays in the gallery after I don't need it anymore.
Question: Is there a way to share an image from the Internet without saving it first?