2017-02-27 91 views
0

我正在android studio中工作。建立一个app我正在使用相机。当我运行我的应用程序的应用程序工作正常。我捕捉它捕捉图像的图像。但是我创建的文件夹没有显示在我的画廊中。我正在将图像保存在我的local storage中,而不是保存在SD CARD中。我很好奇,为什么不创建文件夹,因为它不会给我任何错误,所以它应该在我的画廊。所以我重新启动我的设备,重新启动后,我可以看到我的画廊中的文件夹和其中的图像。我再次打开应用程序并从中获取图像,但图像未再显示在文件夹中。当我从我的应用程序中拍照时,图像目录未创建

下面是我的代码中,我想提出的TA目录保存图像

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
    { 
     if(resultCode == Activity.RESULT_OK) 
     { 
      Bitmap bmp = (Bitmap)data.getExtras().get("data"); 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

      bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
      byte[] byteArray = stream.toByteArray(); 

      // convert byte array to Bitmap 
      Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 

      if(isStoragePermissionGranted()) 
      { 
       SaveImage(bitmap); 
      } 


     } 

} 
private void SaveImage(Bitmap finalBitmap) { 

    String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); 
    Log.v(LOG_TAG, root); 
    File myDir = new File(root + "/captured_images"); 
    myDir.mkdirs(); 
    Random generator = new Random(); 
    int n = 1000; 
    n = generator.nextInt(n); 
    String fname = "Image-" + n + ".jpg"; 
    File file = new File(myDir,fname); 
    if (file.exists())file.delete(); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     finalBitmap.compress(Bitmap.CompressFormat.JPEG,100,out); 
     out.flush(); 
     out.close(); 


    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

下面是我调试的画面

enter image description here

**注:**

由于我使用的是原生相机,所以图片保存在相机胶卷文件夹中,即我的设备中的默认文件夹。但保存的图像不是压缩的图像,压缩图像应保存在我创建的文件夹中。

我坚持它,不知道该怎么做。

任何帮助将不胜感激。

+0

saveImage'方法调用这个'sendBroadcast后'(新的意向书( Intent.ACTION_MEDIA_MOUNTED, Uri.parse( “文件://” + Environment.getExternalStorageDirectory())));' – sajan

+0

尝试并获得这个错误'java.lang.RuntimeException:失败的结果ResultInfo {who = null,request = 1888,result = -1,data = Intent {act = inline-data(has extras)}} to activity' – faisal1208

+0

** Android 4.4 ** ''sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse( “文件://” + Environment.getExternalStorageDirectory())));'' **从Android 4.4的** ''sendBroadcast(新建Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse(“file://”+ Environment.getExternalStorageDirectory())));'' – sajan

回答

2

您需要调用SCANFILE(上下文的背景下,字符串[]路径的String [] MIMETYPES,MediaScannerConnection.OnScanCompletedListener回调)的方法MediaScannerConnection

MediaScannerConnection为应用程序将新创建或下载的媒体文件传递到媒体扫描器服务提供了一种方法。这将使用新保存的媒体更新您的文件夹。

private void SaveImage(Bitmap finalBitmap) { 

     //.... 
     if(!storageDir.exists()){ 
       storageDir.mkdirs(); 
     } 
     //... 
     file.createNewFile(); 
     try { 
      MediaScannerConnection.scanFile(context, new String[] {file.getPath()} , new String[]{"image/*"}, null); 
      FileOutputStream out = new FileOutputStream(file); 
      finalBitmap.compress(Bitmap.CompressFormat.JPEG,100,out); 
      out.flush(); 
      out.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
+0

in'new String [] {“image/*”});'代替'image/*'我将放置我的文件夹名称? – faisal1208

+0

您需要放置MIME类型的数组。在你的情况下,如果它唯一的图像媒体将是图像/ *本身。 – CoderP

+0

好吧,所以不需要改变它? – faisal1208

0

试试这个

private void saveBitmap(Bitmap bitmap) { 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = timeStamp + ".jpg"; 
     File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
     final String fileLoc = storageDir.getAbsolutePath() + "/folderName/" + imageFileName; 
     File file = new File(fileLoc); 
     OutputStream os = null; 
     try { 
      os = new BufferedOutputStream(new FileOutputStream(file)); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); 
     try {`enter code here` 
      os.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

创建文件夹试试这个File folder = new File(“/ sdcard /夹”); if(!folder.exists()) folder.mkdirs(); –

+0

我没有使用'SD'卡,我将图像保存在我的本地存储 – faisal1208