2017-07-07 56 views
0

如何在應用程序中使用相機並將拍攝的照片存儲在應用程序中..我參考了許多教程,但我沒有得到正確的代碼..請幫我解決問題。我的代碼會要求保存選項...當我們重新打開應用程序時,圖片不會保存在應用程序和圖庫中....請幫助做到這一點..我的代碼是..在此代碼中捕獲的圖像存儲在該圖像視圖中...一次android如何掃描或使用相機拍照並將該圖片存儲在我們的應用程序中

this.imageView = (ImageView)this.findViewById(R.id.imageView1); 
    Button photoButton = (Button) this.findViewById(R.id.button1); 
    photoButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     } 
    }) ; 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) { 
     Bitmap photo = (Bitmap) data.getExtras().get("data"); 
     imageView.setImageBitmap(photo); 
    } 

回答

0

這裏是一種將位圖保存到存儲的方法。 imageMainDirectory看起來像「/ MyApp /」。該imageDirectory看起來像「/ MyApp的/ MyImages /」

public boolean saveBitmapToStorage(Bitmap bitmap, String imageMainDirectory, 
             String imageDirectory, String fileName) { 
     boolean result = false; 
     // This is true if the saving of image to the external storage fails. 
     // Then we proceed to the internal storage 
     boolean proceedToInternal; 

     // You need to check if External Storage is writable/readable 
     if (MemoryManager.isExternalStorageWritable() 
       && MemoryManager.isExternalStorageReadable()) { 
     // Check if Memory is equals or greater than 10mb 
      if (MemoryManager.checkSdCardMemory(10)) { 
       File imgdir = new File(Environment.getExternalStorageDirectory() 
         + imageDirectory); 
       File file = new File(imgdir, fileName); 
       // (Optional) replace file if its already existing 
       if (file.exists()) file.delete(); 
       try { 
        FileOutputStream out = new FileOutputStream(file); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out); 
        out.flush(); 
        out.close(); 
        result = true; 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       proceedToInternal = false; 
      } else { 
       proceedToInternal = true; 
      } 
     } else { 
      proceedToInternal = true; 
     } 
     if (proceedToInternal) { 
      if (MemoryManager.checkInternalMemory(10)) { 
       File imgdir = new File(Environment.getDataDirectory() 
         + imageDirectory); 
       File file = new File(imgdir, fileName); 
       // (Optional) replace file if its already existing 
       if (file.exists()) file.delete(); 
       try { 
        FileOutputStream out = new FileOutputStream(file); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out); 
        out.flush(); 
        out.close(); 
        result = true; 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     return result; 
    } 

這裏是內存管理器類:

public class MemoryManager { 

    public static boolean checkSdCardMemory(long desiredMemory) { 
     boolean result = true; 

     StatFs stat = new StatFs(Environment.getExternalStorageDirectory() 
      .getPath()); 
     long bytesAvailable = (long) stat.getBlockSize() 
      * (long) stat.getAvailableBlocks(); 
     long megAvailable = bytesAvailable/1048576; 

     if (megAvailable < desiredMemory) { 
     result = false; 
     } 

     return result; 
    } 

    public static boolean checkInternalMemory(long desiredMemory) { 
     boolean result = true; 

     StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); 
     long bytesAvailable = (long) stat.getBlockSize() 
      * (long) stat.getAvailableBlocks(); 
     long megAvailable = bytesAvailable/1048576; 

     if (megAvailable < desiredMemory) { 
     result = false; 
     } 

     return result; 
    } 

    public static boolean isExternalStorageWritable() { 
     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
     return true; 
     } 
     return false; 
    } 

    /* Checks if external storage is available to at least read */ 
    public static boolean isExternalStorageReadable() { 
     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state) 
      || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     return true; 
     } 
     return false; 
    } 
} 
+0

謝謝... shouldni插入所有的代碼在我的主類 – shwettha

+0

isExternalStorageReadable().createDIRNoMediaFile cnnot解決方法:錯誤 – shwettha

+0

對不起,我刪除了未使用的行。請參閱編輯。 – icaneatclouds

相關問題