请有人告诉我错误!我收到了错误 btmp.compress(Bitmap.CompressFormat.JPEG,80,baos);
从drawable文件夹中获取的PNG图像一切正常但是当我尝试访问卡片图像或任何jpeg时,它会在压缩时出错...
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
我按如下方式编辑了我的代码:
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");
}
但我得到FileNotFoundException。我的SD卡中有image.jpeg文件......有任何建议!
答案 0 :(得分:0)
如果您只需要将文件的内容作为输入流,为什么要绕过丛林?
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();
}
}