从创建URI类型的android文件

从创建URI类型的android文件

本文介绍了从创建URI类型的android文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择的画廊图像,然后转换此图片文件,并通过HttpPost发送它,但我发现总是 FileNotFoundException异常。这是我的code:

选择照片

 公共无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == Activity.RESULT_OK){
            如果(要求code == 1){
                // currImageURI是我用来存放内容的全局变量:
                currImageURI = data.getData();
                //保存currImageUri(URI型),以全局变量。
                。photosHolder.getInstance()setOneIm(currImageURI);
            }
        }
    }
 

转换照片

  //第一次尝试
                myfile文件=新的文件((photosHolder.getInstance()getOneIm())的toString());
                params.put(visit_report [photos_attributes] [0] [文件],MYFILE); **例外这里提出
//第二次尝试
                文件myFile2 =新的文件((photosHolder.getInstance()getOneIm())getPath());
                params.put(visit_report [photos_attributes] [1] [文件],myFile2); **另外这里
 

和那些MYFILES价值,同时debuging:

  MFILE:内容:/com.android.providers.media.documents/document/image%3A19143
myFile2:/文档/图片:19143
 

因此​​,任何帮助?


更新我曾尝试这种解决方案也:

  //从URI真正的路径,然后将其保存(,然后用它来创建文件)
photosHolder.getInstance()setUriString(getRealPathFromURI(currImageURI))。

//在图像的URI转换为图像文件的直接文件系统路径
    私人字符串getRealPathFromURI(URI contentURI){
        字符串结果=;
        尝试 {
        光标光标= getActivity()getContentResolver()查询(contentURI,NULL,NULL,NULL,NULL);
        如果(光标== NULL){//来源是Dropbox的或其他类似的本地文件路径
            结果= contentURI.getPath();
        } 其他 {
            cursor.moveToFirst();
            INT IDX = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            结果= cursor.getString(IDX); //发生异常的位置
            cursor.close(); }
        }赶上(例外五){
            e.printStackTrace();
        }
        返回结果;
    }
 

不过,我得到了 java.lang.IllegalStateException:无法读取行0,列-1从CursorWindow。确保光标从它访问数据之前 IDX VAR ==为 1

此外,我曾尝试@Praneeth卡鲁解决方案,但它的结果总是返回空值。

解决方案

 乌里currImageURI = data.getData();
 

打印currImageURI会给你一些像这样的事情:

 内容://媒体/外部/图片/媒体/ 47
 

但是我们需要的是该特定图像的绝对路径。因此,我们需要从URI真实路径

 公共字符串getRealPathFromURI(上下文的背景下,乌里contentUri){
  光标光标= NULL;
  尝试 {
    的String []凸出= {MediaStore.Images.Media.DATA};
    光标= context.getContentResolver()查询(contentUri,凸出,NULL,NULL,NULL);
    INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    返回cursor.getString(Column_Index中);
  } 最后 {
    如果(光标!= NULL){
      cursor.close();
    }
  }
}
 

现在modifiy你code像

 公共无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == Activity.RESULT_OK){
            如果(要求code == 1){
                // currImageURI是我用来存放内容的全局变量:
                currImageURI = data.getData();
                //保存currImageUri(URI型),以全局变量。
                。photosHolder.getInstance()setOneIm(getRealPathFromURI(getActivity(),currImageURI));
            }
        }
    }

公共字符串getRealPathFromURI(上下文的背景下,乌里contentUri){
  光标光标= NULL;
  尝试 {
    的String []凸出= {MediaStore.Images.Media.DATA};
    光标= context.getContentResolver()查询(contentUri,凸出,NULL,NULL,NULL);
    INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    返回cursor.getString(Column_Index中);
  } 最后 {
    如果(光标!= NULL){
      cursor.close();
    }
  }
}
 

I'm trying to select image from gallery then convert this image to File and send it via HttpPost but I'm getting always FileNotFoundException. This my Code :

Selecting the photo

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                // currImageURI is the global variable I’m using to hold the content:
                currImageURI = data.getData();
                //Save the currImageUri (URI type) to global variable.
                photosHolder.getInstance().setOneIm(currImageURI);
            }
        }
    }

Converting the photo

                // First Try
                File myFile = new File((photosHolder.getInstance().getOneIm()).toString());
                params.put("visit_report[photos_attributes][0][file]",myFile); **The exception Raised here
// second try
                File myFile2 = new File((photosHolder.getInstance().getOneIm()).getPath());
                params.put("visit_report[photos_attributes][1][file]",myFile2); ** Also here

And those are myFiles Value while debuging :

mFile : content:/com.android.providers.media.documents/document/image%3A19143
myFile2 : /document/image:19143

So any help ?


UpdateI have tried this solution also :

//get The real path from uri then save it (and then use it to create the file)
photosHolder.getInstance().setUriString(getRealPathFromURI(currImageURI));

//Convert the image URI to the direct file system path of the image file
    private String getRealPathFromURI(Uri contentURI) {
        String result = "";
        try {
        Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx); // Exception raised HERE
            cursor.close(); }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

But I got java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it and the idx var == to -1

Also I have tried @Praneeth Kalluri Solution but it returns always null as result.

解决方案
 Uri currImageURI = data.getData();

printing currImageURI will give you some thing like this:

content://media/external/images/media/47

But what we need is the absolute path of that particular image . So we need to get real path from uri

public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try {
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

Now modifiy your code like

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                // currImageURI is the global variable I’m using to hold the content:
                currImageURI = data.getData();
                //Save the currImageUri (URI type) to global variable.
                photosHolder.getInstance().setOneIm(getRealPathFromURI(getActivity(),currImageURI));
            }
        }
    }

public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try {
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

这篇关于从创建URI类型的android文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:24