English 中文(简体)
如何使用外部文件管理器 Apps Android 获取文件路径?
原标题:How to get path to file using some external file managers apps Android?
  • 时间:2012-05-24 10:11:49
  •  标签:
  • java
  • android

能否从Sd卡中获取文件, 该文件是使用第3个应用程序( 文件管理器)选择的?

我的意思是,我有活动 与按钮打开文件, 当用户按下打开时, 它显示他建议使用其他应用程序打开文件, 当他选择打开时, 我就可以回到我的活动, 并使用文件路径?

最佳回答

使用此

button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    SELECT_PICTURE);
        }
    });

在活动中添加两种方法

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {

            Uri selectedImageUri = data.getData();
            String s1 = data.getDataString();
            //String s1 = selectedImageUri.getPath();
            Log.e("GetPath",s1);
            //s1 = s1.replaceAll("file://","");
            //Uri a = Uri.fromParts(s1,null,null);
            Log.e("OK",""+selectedImageUri);
            //Log.e("A",""+a);

            selectedImagePath = getPath(selectedImageUri);
            if(selectedImagePath==null && s1 != null)
            {
                selectedImagePath = s1.replaceAll("file://","");
            }
        //  selectedImagePath = getPath(a);




            Intent intent = new Intent(this, PhotoEditorActivity.class);
            intent.putExtra("path", selectedImagePath);
            startActivity(intent);
            finish();


        }
                  }
               }


///////////////////////////////////
   public String getPath(Uri uri) {

    try{
    String[] projection = { MediaStore.Images.Media.DATA };
    Log.e("OK 1",""+projection);
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    Log.e("OK 2",""+cursor);
    if(cursor==null)
    {
        return null;

    }
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    Log.e("OK 3",""+column_index);
    cursor.moveToFirst();
    Log.e("OK 4",""+cursor.getString(column_index));
    return cursor.getString(column_index);
    }
    catch(Exception e)
    {
        Toast.makeText(PhotoActivity.this, "Image is too big in resolution please try again", 5).show();
        return null;
    }

}

并添加此整数作为类成员

  private static final int SELECT_PICTURE = 1;

享受沸水的滋味吧!

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签