2013-04-05 2 views
0

캡처 한 .bmp 파일을 sdcard에 저장하려고합니다. 다음은 이에 대한 책임이 코드 조각입니다 :안드로이드 저장 bmp 이미지

String root = Environment.getExternalStorageDirectory().toString(); 
    File mFolder = new File(root + "/mFolder"); 

    if (!mFolder.exists()) 
    { 
     mFolder.mkdir(); 
    } 
    String strF = mFolder.getAbsolutePath(); 
    File mSubFolder = new File(strF + "/MyApp-SubFolder"); 

    if (!mSubFolder.exists()) 
    { 
     mSubFolder.mkdir(); 
    } 
    String s = "myfile.png"; 

    File f = new File(mSubFolder.getAbsolutePath(),s); 
    String strMyImagePath = f.getAbsolutePath(); 
    FileOutputStream fos = null; 
    try 
    { 
     fos = new FileOutputStream(f); 
     bmp.compress(Bitmap.CompressFormat.PNG,70, fos); 

     fos.flush(); 
     fos.close(); 
     Log.d("asd", "yeah!"); 
    // MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); 
    }catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

는하지만 오류가 :

images are invalid and its size are 0kb

내가 잘못하고있는 중이 야 무슨 일이?

+0

문자열의 = "myfile.png"; ? 당신은 파일이 – Hasham

+0

이 \t 파일 storagePath = 새로운 파일 ( \t \t \t \t \t Environment.getExternalStorageDirectory() + "/ MyApp를-하위 폴더 /") 시도 .BMP이고; – Hasham

답변

2

private boolean SaveToSD() { 

     String imageName = null; 

     Bitmap sourceBitmap = ((BitmapDrawable) img.getDrawable()).getBitmap(); 

     boolean imageSaved = false; 

     if (sourceBitmap != null && !sourceBitmap.isRecycled()) { 
      File storagePath = new File(
        Environment.getExternalStorageDirectory() + "/iGridu/"); 

      if (!storagePath.exists()) { 
       storagePath.mkdirs(); 
      } 

      int count = storagePath.list().length; 

      Log.i("SaveToSD count", "" + count); 

      imageName = String.valueOf(count + 1) + "_igridu"; 

      FileOutputStream out = null; 
      File imageFile = new File(storagePath, String.format("%s.jpg", 
        imageName)); 
      try { 
       out = new FileOutputStream(imageFile); 
       imageSaved = sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 
         90, out); 
       out.flush(); 
       out.close(); 
      } catch (Exception e) { 
       Log.e("SaveToSD ", "Unable to write the image to gallery" + e); 

      } 

      ContentValues values = new ContentValues(3); 
      values.put(Images.Media.TITLE, imageName); 
      values.put(Images.Media.MIME_TYPE, "image/jpeg"); 
      values.put("_data", imageFile.getAbsolutePath()); 

      getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); 
     } 

     return imageSaved; 
    } 
+0

예, 매니페스트에 추가했지만 작동하지 않습니다. 이미지가 잘못되었습니다 .. – user2249145

+0

감사합니다. 그러나 코드가 작동하지 않습니다. – user2249145

+0

지금 문제가 뭐니? – Hasham

0

당신은 당신의 Manifest.xml에 넣고해야 시도

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

추가했지만 아무 것도 변경하지 않았습니다. – user2249145