Android convert jpg image to png

In Android, PGN images are widely used. One important reason is that PNG format supports transparency and compresses image with keeping high quality. If have JPEG images and want to convert them to PNG format, this Android is useful to you.
To convert an JPEG image to an PNG image, first you load the JPEG image file into a Bitmap object using the decodeFile() method of the BitmapFactory class. Then you use the compress() method of the Bibmap to write the compressed version of the image to a FileOutputStream. The method returns true if the image is successfully converted to a specified format. Otherwise, it returns false. With the compress() method, you can specify the PNG format, image quality, and the object of FileOutputStream to save the converted image to a new file. The quality is an integer value from 0 to 100. If you compress the image with 0 quality, you get a small size image. 100 in quality means compression for max quality.



Now to have example application on converting a JPG image to a PNG image, you create Android project. Then in the AndroidManifest.xml file, you declare an intent-filter to allow the application to accept a JPG image selected from the file explorer. The JPG image will be converted to PNG format.

<intent-filter>
           <action android:name="android.intent.action.VIEW" />
           <category android:name="android.intent.category.DEFAULT" />
           <data android:mimeType="image/*" />
          
        </intent-filter>

Here is the code written in the MainActivity class to receive the path of image selected from the file explorer, convert it, and save the new converted image in a new file called convertedImg.png in sdcard folder.


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity{

String filepath;

protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     Intent intent=getIntent();
     boolean success=false;
     if(intent!=null){
     String action=intent.getAction();
     String type=intent.getType();
     //Log.i("Main",type);
     if((Intent.ACTION_VIEW.equals(action)) && type.endsWith("jpeg")){
     // Get the file path from the intent object
     filepath=intent.getData().getPath();
     try {
     Bitmap bmp = BitmapFactory.decodeFile(filepath);// Create Bitmap object for the original image
     // Crate new converted image file object
     File convertedImage = new File(Environment.getExternalStorageDirectory()+"/convertedimg.png");
     // Create FileOutputStream object to write data to the converted image file
     FileOutputStream outStream=new FileOutputStream(convertedImage);
     // Keep 100 quality of the original image when converting
     success=bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    
     }
     }
    
     if(success){
     Toast.makeText(this,"Converting is successful.",Toast.LENGTH_SHORT).show();
     }
     else{
     Toast.makeText(this,"Converting is unsucessful.",Toast.LENGTH_SHORT).show();
     }
    
   }


}

Before you run the application, do not forget to declare WRITE_EXTERNAL_STORAGE permission to the AndroidManifest.xml file.



Android-convert-jpg-image-to-png

Posted by: Dara | post date: 12-31-2014 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.