简体   繁体   中英

Android SDcard Image to byte error

please can anyone tell me the error ! i get error in the line btmp.compress(Bitmap.CompressFormat.JPEG, 80, baos);

everything works fine for an PNG image taken from the drawable folder but when i try to access the card image or any jpeg it gives error in compress...

    public class img2byte extends Activity {

TextView tv;
Bitmap btmp = null;
ByteArrayOutputStream baos = null;
byte[] bytes;
File file=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView) findViewById(R.id.textView1);

    file=new File(Environment.getExternalStorageDirectory()+"/image.jpeg");
    Uri abc =Uri.fromFile(file);
    tv.setText(abc.toString()+"\n");

    Log.i("da","Got Uri");

    try {
        Log.i("da","before btmp");
        btmp = Media.getBitmap(getContentResolver(),abc);
    } catch (FileNotFoundException e) {
        tv.setText("file not found");
        System.out.println("sjadhad");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    baos = new ByteArrayOutputStream();
    btmp.compress(Bitmap.CompressFormat.JPEG, 80, baos);
    Log.i("da","compressed");

    bytes = baos.toByteArray();
    tv.append(""+bytes.length+"\n");
    tv.append(baos.toString());
    for(int i=0; i<bytes.length; i++) 
        tv.append(""+bytes[i]);
    }

}

Logcat :

03-18 10:46:18.699: ERROR/AndroidRuntime(20379): FATAL EXCEPTION: main
03-18 10:46:18.699: ERROR/AndroidRuntime(20379): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dhiraj.img2btye/com.dhiraj.img2btye.img2byte}: java.lang.NullPointerException
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.os.Looper.loop(Looper.java:143)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.app.ActivityThread.main(ActivityThread.java:4263)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at java.lang.reflect.Method.invoke(Method.java:507)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at dalvik.system.NativeStart.main(Native Method)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379): Caused by: java.lang.NullPointerException
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at com.dhiraj.img2btye.img2byte.onCreate(img2byte.java:49)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
03-18 10:46:18.699: ERROR/AndroidRuntime(20379):     ... 11 more

I edited my code as follows :

File file = new File(Environment.getExternalStorageDirectory(),"/image.jpeg");
    Log.i("da", "Got file");
    InputStream in = null;
    OutputStream out=null;
    try {
        in = new BufferedInputStream(new FileInputStream(file));
    } catch (FileNotFoundException e) {     

        Log.i("da", "file not found");
    }

but i get FileNotFoundException. i have the file image.jpeg in my sdcard... any suggestions !

If all you need is to get the file's content as an input stream, why go round the bush?

File file = new File(Environment.getExternalStorageDirectory(), "image.jpeg");
InputStream in = null;
try {
    in = new BufferedInputStream(new FileInputStream(file));
    // Read from the InputStream and send over BT
} finally {
    if (in != null) {
        in.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM