2013-07-02 70 views
0

我创建一个图像转换应用程序,需要用户从设备中选择图像文件,即时通讯使用模拟器,当我开始从图库中选择一个文件的意图时,它显示在他们的文件夹,但在点击一个文件夹的图像,它只是重新打开画廊,这里不是文件夹是我的代码获取图像内容无法打开图库文件夹

private void showFileChooser() { 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 

    try { 
     startActivityForResult(
       Intent.createChooser(intent, getString(R.string.selectf)), 
       FILE_SELECT_CODE); 
    } catch (ActivityNotFoundException ex) { 
     // Potentially direct the user to the Market with a Dialog 
     Toast.makeText(this, "Please install a File Manager.", 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK && resultCode == Activity.RESULT_OK) { 
      // Get the Uri of the selected file 
      Uri uri = data.getData(); 
      //Log.d(TAG, "File Uri: " + uri.toString()); 
      // Get the path 
      try { 
       Path = getPath(this, uri); 
       if( Path.endsWith("png") || Path.endsWith("gif") || Path.endsWith("jpg") || Path.endsWith("bmp")){ 
        startprogress2(); 
        progressDialog.onStart(); 
        asyncTask = new loadAction(); 
        asyncTask.execute(); 
       } 
       else{ 
        Toast.makeText(this, R.string.selecti, 
          Toast.LENGTH_SHORT).show(); 
       } 
      }catch (URISyntaxException e) { 
       e.printStackTrace(); 
       failtoloadtoast(); 
      }catch(Exception e){ 
       e.printStackTrace(); 
       failtoloadtoast(); 
      } 
     }else{ 
      failtoloadtoast(); 
     } 


    super.onActivityResult(requestCode, resultCode, data); 
} 

记住我必须使用户能够从一个文件管理器还可以选择图像文件不仅画廊有些格式不被画廊阅读。

回答

0

使用这样:

意图:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.setType("image/*"); 
      startActivityForResult(intent, 0); 

要获取该结果:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

     switch(requestCode) { 
     case 0: 
      if(resultCode == RESULT_OK){ 
       Uri selectedImage = imageReturnedIntent.getData(); 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
       Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
       cursor.moveToFirst(); 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       //file path of selected image 
       filePath = cursor.getString(columnIndex); 
       File f = new File(filePath); 
       filename= f.getName(); 

       Toast.makeText(SiteViewFieldCreate.this, "Your Path:"+filePath, 2000).show(); 
       Toast.makeText(SiteViewFieldCreate.this, "Your Filename:"+filename, 2000).show(); 
       cursor.close(); 

      } 
      break; 
    } 
    } 

Reference。 希望这会给你一些解决方案。

+0

no ..没有区别,我有一种感觉,这只是在模拟器上 –

+1

请检查设备。因为这段代码适合我。 – Nirmal