android如何使用相机扫描或拍照并在我们的应用程序中存储该图片

时间:2017-07-07 02:15:35

标签: 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);
    }

1 个答案:

答案 0 :(得分: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;
    }

这是Memory Manager类:

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;
   }
}