java - 如何获取从图库中选择的图像的目录和文件名

标签 java android file path dir

我需要使用这个功能,

java.io.File.File(File dir, String name)

public File (File dir, String name)

Added in API level 1 Constructs a new file using the specified directory and name. Parameters dir the directory where the file is stored. name the file's name. Throws NullPointerException if name is null.

现在我们可以看到它需要文件目录和名称作为参数。现在我有意从图库中选择一张图像。如何获取所选文件的目录和名称?

这是 fragment :

public File String_to_File(String img_url) {

        try {
            File rootSdDirectory = Environment.getExternalStorageDirectory();

---------------------------------------------------------------------------------------
            **casted_image = new File(rootSdDirectory, "attachment.jpg");**
---------------------------------------------------------------------------------------

            if (casted_image.exists()) {
                casted_image.delete();
            }
            casted_image.createNewFile();

            FileOutputStream fos = new FileOutputStream(casted_image);

            URL url = new URL(img_url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.connect();
            InputStream in = connection.getInputStream();

            byte [ ] buffer = new byte [1024];
            int size = 0;
            while ((size = in.read(buffer)) > 0) {
                fos.write(buffer, 0, size);
            }
            fos.close();
            return casted_image;

        } catch (Exception e) {

            System.out.print(e);
            // e.printStackTrace();

        }
        return casted_image;
    }

    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);

         if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
             Uri selectedImage = data.getData();
             String[] filePathColumn = { MediaStore.Images.Media.DATA };

             Cursor cursor = getContentResolver().query(selectedImage,
                     filePathColumn, null, null, null);
             cursor.moveToFirst();

             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
             String picturePath = cursor.getString(columnIndex);
             cursor.close();

             iv.setImageURI(selectedImage);

             // String picturePath contains the path of selected Image
         }
    }

应该如何将正确的参数传递给两行之间的函数?

我正在引用sharing-text-image-in-twitter-android-example 。一切都很好,除了我想使用我自己选择的图像而不是他的 URL 图像。请帮忙。

最佳答案

通过 Intent.putExtra() 方法在 Intent 上设置 EXTRA_ALLOW_MULTIPLE 选项:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

上面的代码应如下所示:

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE );
//intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

注意:EXTRA_ALLOW_MULTIPLE 选项仅在 Android API 18 及更高版本中可用。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE  && resultCode == RESULT_OK && null != data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage,
         filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    casted_image = new File(picturePath);
    cursor.close();
    // String picturePath contains the path of selected Image
  }
}

关于java - 如何获取从图库中选择的图像的目录和文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25760676/

相关文章:

java - Jni jintArray 参数错误

java - 将SceneForm/ARCore添加到Gradle时,Android App无法编译

java - 在 Java 中使用递归将字符串转换为反向字符串

java - 在 Activity 中发送和添加回收器 View 数据时遇到问题

在 C 中将用户名和密码从一个文件复制到另一个文件

Python 在 open with 函数中添加空行

java - 服务器上生成的带有 MediaType.TEXT_EVENT_STREAM 的事件何时会传递给客户端上的订阅者

java - 静默字符串之谜(服务器从Android客户端接收空字符串)

android - 在 Android 上,如何通过窗口上的元素(包括 ListView)显示背景图像?

javascript - 如何将其设置为 "save as HTML"而不是 "download as HTML"?