2014-04-04 5 views
0

에 이미지를 저장 할 수없는 압축이안드로이드 이미지 비트 맵 내가했던 SD 카드에 이미지를 저장해야 SD 카드

Bitmap bmImg = imageCacher 
        .getBitmap("http://leewayinfotech.com//mobile//ebook//uploaded_images//abstract//165//16521396377593_4.jpg"); 
String str = data[currentItem]; 

Bitmap bmImg = imageCacher.getBitmap(str); 

try { 
     String path1 = android.os.Environment.getExternalStorageDirectory().toString(); 
     Log.i("in save()", "after mkdir"); 
     File file=new File(path1 + "/"+appName); 
     if (!file.exists()) 
     file.mkdirs(); 
     filename = new File(file.getAbsolutePath()+"/"+imageName+".jpeg"); 
     Log.i("in save()", "after file"); 
     FileOutputStream out = new FileOutputStream(filename); 
     Log.i("in save()", "after outputstream"); 
     bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out); 
     out.flush(); 
} catch(Exception e) { 

} 

하지만 내 로그 캣는 말한다 :

에서 오류를주는

04-04 16:23:32.301: I/in save()(14452): after outputstream 
04-04 16:23:32.301: W/System.err(14452): java.lang.NullPointerException 
04-04 16:23:32.301: W/System.err(14452): at com.leeway.hdwallpaper.My_Pagging_Activity$Image_Pager_Adpter$1.onLongClick(My_Pagging_Activity.java:219) 
04-04 16:23:32.301: W/System.err(14452): at android.view.View.performLongClick(View.java:4207) 
04-04 16:23:32.301: W/System.err(14452): at android.view.View$CheckForLongPress.run(View.java:17174) 
04-04 16:23:32.301: W/System.err(14452): at android.os.Handler.handleCallback(Handler.java:643) 
04-04 16:23:32.301: W/System.err(14452): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-04 16:23:32.301: W/System.err(14452): at android.os.Looper.loop(Looper.java:137) 
04-04 16:23:32.301: W/System.err(14452): at android.app.ActivityThread.main(ActivityThread.java:4803) 
04-04 16:23:32.301: W/System.err(14452): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 16:23:32.301: W/System.err(14452): at java.lang.reflect.Method.invoke(Method.java:511) 
04-04 16:23:32.301: W/System.err(14452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
04-04 16:23:32.301: W/System.err(14452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
04-04 16:23:32.301: W/System.err(14452): at dalvik.system.NativeStart.main(Native Method) 
           out.close(); 
           Log.i("in save()", "after outputstream closed"); 

bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);

+1

비트 맵 객체 bmImg이 null입니다. –

답변

2

시험해보기 :

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),  "AppName"); 
     if (!file.exists()) { 
      file.mkdirs(); 
         } 
String image_name = "IMG_Name.jpg"; 
File outputFile = new File(file, image_name); 
FileOutputStream fos = new FileOutputStream(outputFile); 
     fos.flush(); 
     fos.write(bytes.toByteArray()); 
     fos.close(); 
+0

해결책에 대해 jyomin에게 감사드립니다. 그것은 나를 위해 일했습니다 :)하지만 당신의 접근 방식과 bitmap.compress의 차이점은 무엇입니까? 설명해 주시겠습니까? – Luqman