2014-07-17 17 views
1

我正在开发一个简单的图库应用程序,其中我搜索包含图像的文件夹,然后显示该特定文件夹的图像。首先,我使用Environment.getExternalStorageDirectory(),然后递归搜索具有图像的文件夹。它工作的很好的模拟器和设备。当我的客户端在Nexus 4上安装应用程序时,无法加载任何内容。我看到一些说Nexus系列没有任何外部SD插槽的帖子。我没有用于调试的Nexus 4,客户端也是非技术型的。他可以自己排查问题的原因。任何人都可以在这方面提供帮助吗?我认为问题出在Environment.getExternalStorageDirectory()调用中,这不适用于Nexus。任何想法如何解决这个问题? 这里是代码剪断我使用:获取Nexus 4上的所有图库图像

private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File cacheDir = null; 
     if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { 
      cacheDir=new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(), "MyImages"); 
     } 
     else { 
      cacheDir = getCacheDir(); 
     } 
     if(!cacheDir.exists()) { 
      cacheDir.mkdir(); 
     } 
//  File storageDir = Environment.getExternalStoragePublicDirectory(
//    Environment.DIRECTORY_PICTURES); 
     File image = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       cacheDir  /* directory */ 
       ); 

     // Save a file: path for use with ACTION_VIEW intents 
     mCurrentPhotoPath = "" + image.getAbsolutePath(); 
     return image; 
    } 

    public void dispatchTakePictureIntent() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     // Ensure that there's a camera activity to handle the intent 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      // Create the File where the photo should go 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
      } catch (IOException ex) { 
       // Error occurred while creating the File 
       ex.printStackTrace(); 
      } 
      // Continue only if the File was successfully created 
      if (photoFile != null) { 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
         Uri.fromFile(photoFile)); 
       startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
      } 
     } 
    } 

在这里,我已经加入清单的权限:

<uses-permission 
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
    android:maxSdkVersion="18" /> 

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

<uses-feature 
    android:name="android.hardware.camera" 
    android:required="true" /> 

谢谢!

+0

请参阅:http://stackoverflow.com/questions/7616974/how-to-check-internal-and-external-storage-if-exist – Shadesblade

回答

0

我想你应该尝试一下MediaStore.Images组件。使用光标加载图像。见文档:http://developer.android.com/reference/android/provider/MediaStore.html

+0

是的,我不得不改变我的方法媒体查询,它工作正常。但现在我面临另一个问题。该应用程序具有使用相机捕捉照片的功能。我正在尝试创建文件,然后将文件信息传递给相机意图。再次在其他设备上工作,但在Nexus 4上遇到问题。应用程序无法创建文件。我试过环境。 getexternalstoragedirextory和getCacheDir都失败了。任何想法如何在Nexus系列中为相机意图创建文件...? –

+0

仔细检查您是否在清单中请求正确的权限,并给我们一些LogCat涂鸦... :) –

+0

我已更新我的问题。请看看它。另外我没有Nexus 4手机。所以我不能在这里提供LogCat输出。谢谢 –

相关问题