Cursor cursor = getContentResolver().query(data.getData(), {MediaStore.Images.Media.DATA}, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 报错 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
时间: 2023-11-27 12:49:49 浏览: 185
这个错误通常是因为查询返回的Cursor中没有指定的列名,你可以检查一下查询语句是否正确,确保查询到的列名与你指定的列名相同。另外,你可以尝试在查询语句中指定需要查询的列和排序方式,例如:
```java
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(data.getData(), projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String filePath = cursor.getString(columnIndex);
cursor.close();
// do something with filePath
}
```
这个例子中,我们使用了`getColumnIndexOrThrow()`方法来获取指定列的索引,如果指定的列不存在会抛出异常。同时,我们还检查了Cursor是否为null,并在使用完之后关闭了它。
相关问题
public List<ImageItem> getAllImages(Context context) { List<ImageItem> data = new ArrayList<>(); int id = 0; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null); if (cursor.moveToFirst()) { do { String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE)); String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); ImageItem images = new ImageItem(id, title, url); data.add(images); id++; } while (cursor.moveToNext()); cursor.close(); } return data; }
很高兴为您提供帮助。这里是获取所有图片的代码:使用ContentResolver查询MediaStore.Images.Media.EXTERNAL_CONTENT_URI,遍历查询结果,将查询到的图片信息封装到ImageItem中,最后返回ImageItem的列表。
请解释以下代码@SuppressLint("SetTextI18n") protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); ImageView img_result=(ImageView) findViewById(R.id.pic); switch (requestCode) { case TAKE_PHOTO: if (resultCode == RESULT_OK) { //将图片解析成Bitmap对象,并把它显现出来 // String filePath = getFilesDir().getAbsolutePath()+"/image.jpeg"; // bitmap = BitmapFactory.decodeFile(filePath); //注意bitmap,后面再decode就会为空 try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); } catch (FileNotFoundException e) { e.printStackTrace(); } // bitmap = BitmapFactory.decodeFile(picpath); picpath=imageUri.getPath().toString(); Log.e("", imageUri.getAuthority()); Log.e("picpath",picpath); @SuppressLint("SdCardPath") String fileName = picpath; img_result.setImageBitmap(bitmap); img_result.invalidate(); } break; case SCAN_OPEN_PHONE: if (resultCode == RESULT_OK){ Uri selectImage=data.getData(); String[] FilePathColumn={MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectImage, FilePathColumn, null, null, null); cursor.moveToFirst(); //从数据视图中获取已选择图片的路径 int columnIndex = cursor.getColumnIndex(FilePathColumn[0]); picpath = cursor.getString(columnIndex); Log.e("picpath",picpath); cursor.close(); bitmap = BitmapFactory.decodeFile(picpath); img_result.setImageBitmap(bitmap); img_result.invalidate(); } break; default: break; } },并说明如何把得到的照片上传到mysql中
这段代码是在处理Android中的相机和相册的操作,根据requestCode的不同,分别处理拍照和选择相册的情况。其中,将得到的照片解析成Bitmap对象,并显示在ImageView中。
要将得到的照片上传到MySQL中,需要先将Bitmap对象转换成字节数组,然后将字节数组上传到服务器。具体实现可以参考以下步骤:
1. 将Bitmap对象转换成字节数组:
```
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
```
2. 将字节数组上传到服务器:
可以使用HttpURLConnection或者OkHttp等网络库进行上传,具体实现可以参考以下代码:
```
URL url = new URL("http://yourserver.com/upload.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.setRequestProperty("Content-Length", String.valueOf(imageBytes.length));
OutputStream os = conn.getOutputStream();
os.write(imageBytes);
os.flush();
os.close();
```
其中,upload.php是服务器端的脚本,用于接收上传的字节数组并保存到MySQL中。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)