2017-07-07 81 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

相关问题