2015-02-26 2 views
2

Мне нужно сохранить изображение в пользовательскую папку на моем телефоне. мой кодСохранить изображение в пользовательской папке android on onPictureTaken

@Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      if (data != null) { 
FileOutputStream outStream = null; 
       try { 
        Random generator = new Random(); 
        int n = 0000; 
        n = generator.nextInt(n); 
        String fName = "Image" + n + ".jpg"; 
        outStream = new FileOutputStream(String.format(fName)); 
        outStream.write(data); 
        outStream.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       camera.startPreview(); 
} 

не работает

+0

есть исключение или изображение не появляется? – Radinator

ответ

6
@Override 
      public void onPictureTaken(byte[] data, Camera camera) { 
       if (data != null) { 
    Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length); 

    if(bitmap!=null){ 

         File file=new File(Environment.getExternalStorageDirectory()+"/dirr"); 
         if(!file.isDirectory()){ 
          file.mkdir(); 
         } 

         file=new File(Environment.getExternalStorageDirectory()+"/dirr",System.currentTimeMillis()+".jpg"); 


         try 
         { 
          FileOutputStream fileOutputStream=new FileOutputStream(file); 
          bitmap.compress(Bitmap.CompressFormat.JPEG,100, fileOutputStream); 

          fileOutputStream.flush(); 
          fileOutputStream.close(); 
         } 
         catch(IOException e){ 
          e.printStackTrace(); 
         } 
         catch(Exception exception) 
         { 
          exception.printStackTrace(); 
         } 

        } 
    } 
} 


Dont forget to add permission: 

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

Спасибо за код. Работает отлично :) –

+0

Ваш прием ... :-) – droidd

0

Вы можете найти анс из ниже кода

@Override 
public void onPictureTaken(byte[] data, Camera camera) { 
if (data != null) { 
Bitmap m_bitmap = BitmapFactory.decodeByteArray(data , 0, data .length); 
String destinationPath = Environment.getExternalStorageDirectory() + "/" + "Image.jpg"; 
if (m_bitmap != null || destinationPath != null) 
{ 
FileOutputStream m_out = new FileOutputStream(destinationPath); 
m_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, m_out); 
} 
} 

Надежда этот код поможет вам

0

Вы должны сначала создать папку вы хотите сохранить файл. Итак, в папке sdk для Android вы создаете папку MediaFiles. В этой папке вы можете создать свой файл.

Вот код, который вы можете попробовать.

@Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      if (data != null) { 
       FileOutputStream outStream = null; 
       try { 
        Random generator = new Random(); 
        int n = 0000; 
        n = generator.nextInt(n); 
        String fName = "Image" + n + ".jpg"; 
        outStream = new FileOutputStream(GetPathForFileName(fName)); 
        outStream.write(data); 
        outStream.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       camera.startPreview(); 
} 

private String GetPathForFileName(String fileName) 
    { 
     try 
     { 
      String savePath = GetSaveFolderFromConfiguration(); 

      File file = new File(savePath + "/" + fileName); 

      return savePath + "/" + fileName; 
     } 
     catch (Exception ex) 
     { 
      ExceptionForm.ShowException(ex); 
     } 
     return null; 
    } 

public static String GetSaveFolderFromConfiguration() throws Exception 
    { 
     String path = Environment.getExternalStorageDirectory().toString() + "/MediaFiles"; 
     File f = new File(path); 
     if (f.exists()) 
      return path; 
     else 
      f.mkdir(); 
     return path; 
    } 
Смежные вопросы