Android Marshmallow 6.0图片文件路径无法写入SD卡

时间:2016-08-12 10:24:10

标签: android android-6.0-marshmallow filepicker

我在我的应用程序中集成了文件选择器。所以我设置下面的代码,我使用这个库。 library

public class FileChooserExampleActivity extends Activity {

private static final String TAG = "FileChooserExampleActivity";

private static final int REQUEST_CODE = 6384;

private static final int REQUEST_WRITE_STORAGE = 112;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create a simple button to start the file chooser process
    Button button = new Button(this);
    button.setText(R.string.choose_file);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Display the file chooser dialog
            showChooser();
        }
    });


    setContentView(button);
}




private void showChooser() {
    // Use the GET_CONTENT intent from the utility class
    Intent target = FileUtils.createGetContentIntent();
    // Create the chooser Intent
    Intent intent = Intent.createChooser(
            target, getString(R.string.chooser_title));
    try {
        startActivityForResult(intent, REQUEST_CODE);
    } catch (ActivityNotFoundException e) {
        // The reason for the existence of aFileChooser
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE:
            // If the file selection was successful
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    // Get the URI of the selected file
                    final Uri uri = data.getData();
                    Log.i(TAG, "Uri = " + uri.toString());
                    try {
                        // Get the file path from the URI
                        final String path = FileUtils.getPath(this, uri);
                        Toast.makeText(FileChooserExampleActivity.this,
                                "File Selected: " + path, Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        Log.e("FileSelectorTestActivity", "File select error", e);
                    }
                }
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

}

permissin

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

当我在Android Marshmallow 6.0设备上运行上面的代码并且我从sdcard获取图像时,我选择的文件路径值为 null ,所以任何想法如何解决这个问题?你的所有建议都很明显

0 个答案:

没有答案