2014-12-07 141 views
-1

我有一个自定义相机Android应用程序,根据Google教程将捕获的图像保存到外部存储器中,然后Media Scanner触发Gallery来检测它们。在内部存储器中存储图像的位置?

但是我对此没有SD卡插槽,而不是外部存储器LG G2新的客户端 - 仅限内部。

我向他解释,我只能让我的应用程序存储图像的应用程序的内部缓存,并且他会通过某种根浏览器访问它们。

但他声称,其他购置相机的应用程序存储他们的图像,以便Gallery可以检测到它们。怎么样?请帮助 - 我需要让我的应用能够做到这一点。

谢谢!

UPD:以下是我的代码,为其他设备和主要处理外部存储器的工作原理:

public static File getBaseFolder() { 
    File f; 
    f = null; 
    try { 
     f = Environment 
       .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    } catch (Exception e) { 
     Utils.toasterLong("Error accessing storage: " + e.getMessage()); 
    } 
    return f; 
} 

public static File getImageFolder() { 
    File f; 
    f = null; 
    try { 
     f = new File(getBaseFolder(), "Magic"); 
    } catch (Exception e) { 
     Utils.toasterLong("Error accessing storage: " + e.getMessage()); 
    } 
    return f; 
} 

/** Create a file Uri for saving an image or video */ 
private static Uri getOutputMediaFileUri(int type) { 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

/** Create a File for saving an image or video */ 
private static File getOutputMediaFile(int type) { 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 

    File mediaStorageDir = getImageFolder(); 
    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d(TAG, "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss") 
      .format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator 
       + "IMG_" + timeStamp + ".jpg"); 
    } else if (type == MEDIA_TYPE_VIDEO) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator 
       + "VID_" + timeStamp + ".mp4"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 

和代码的一部分图像写入到外部存储器,并触发扫描仪:

{ 
pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 
       if (pictureFile == null) { 
        writeErrorDirty = true; 
        if (MyDebug.LOG) 
         Log.d(TAG, 
           "Error creating media file, check storage permissions"); 

       } else if (MyDebug.LOG) 
        Log.d(TAG, "Valid path=" + pictureFile.getAbsolutePath()); 

       FileOutputStream out = null; 
       try { 
        out = new FileOutputStream(pictureFile); 
        picTaken_mod.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       } catch (Exception e) { 
        e.printStackTrace(); 
        writeErrorDirty = true; 
       } finally { 
        try { 
         if (out != null) { 
          out.close(); 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
         writeErrorDirty = true; 
         if (MyDebug.LOG) 
          Log.d(TAG, 
            "Error writing media file, check storage permissions"); 
        } 
       } // end try 

try { 
       MediaScannerConnection.scanFile(grandContext, 
         new String[] { pictureFile.getAbsolutePath() }, null, 
         new MediaScannerConnection.OnScanCompletedListener() { 
          public void onScanCompleted(String path, Uri uri) { 
           // now visible in gallery 
          } 
         }); 
      } catch (Exception e) { 
       if (MyDebug.LOG) 
        Log.d(TAG, 
          "Error broadcasting the image: " + e.getMessage()); 
       // writeErrorDirty = true; 
      } 
} 

回答

1

但是我对此没有SD卡插槽,而不是外部存储器LG G2新的客户端 - 仅限内部。

您在该设备上同时有internal storageexternal storage。您可能没有removable storage,但可移动存储不是内部存储,也不是外部存储。

请理解什么Android SDK中是指作为内部存储器和外部存储器被绑定到Android SDK作为不一定与显示给用户(例如,在设置)什么样的信息排队。

但他声称其他购买的相机应用程序存储他们的图像,以便图库可以检测到它们。怎么样?

他们写的图像外部存储,然后使用MediaScannerConnection通知画廊和使用MediaStoreContentProvider这些文件是否有其他应用程序。

+0

当你描述: - 他们写的图像外部存储,然后使用MediaScannerConnection通知画廊和使用MediaStore的ContentProvider,该文件有其他应用程序。 - 你描述了我的应用。它适用于数百种设备,但不适用于此G2 G2 – rommex 2014-12-07 16:26:26

+0

@rommex:然后,您要么不在G2上写入外部存储器,要么G2存在某种错误。您可能会考虑编辑您的问题,以发布您用于编写该文件的代码,并通过'MediaStore'索引它。 – CommonsWare 2014-12-07 16:31:29

+0

请在上面编辑的帖子中查看我的代码,谢谢! – rommex 2014-12-07 18:03:54

相关问题