0
我是android開發新手。我正在處理圖庫中的圖像和視頻。我從圖庫上傳圖片或視頻。我得到遊標值返回null。無法從部分Android手機上的圖片庫中上傳圖像和視頻
首先
1)爲什麼出現問題時上傳圖片或視頻?
2)如何解決這個錯誤?
我用來上傳和gallery`圖像和視頻
OpenGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mode = "sms";
gocodercamera.stopPreview();
isCameraMenuVisible = false;
CameraMenu.setVisibility(View.GONE);
Intent galleryIntent = new Intent(/*Intent.ACTION_GET_CONTENT*/Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/* video/*");
startActivityForResult(galleryIntent, 300);
}
});
`
OnActivityResult`
/**Picking file from Gallery*/
if (requestCode == 300) {
if (resultCode == RESULT_OK) {
try {
gocodercamera.setVisibility(View.VISIBLE);
gocodercamera.startPreview();
if (data != null) {
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 filepath = cursor.getString(columnIndex);
cursor.close();
if (filepath.endsWith(".jpg") || filepath.endsWith(".jpe") || filepath.endsWith(".jpeg") || filepath.endsWith(".png")) {
imagefilepath = filepath;
// uriList.add(imagefilepath);
// typesList.add("image");
Uri getCompressedUriPath = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
String getCompressedPath = compressImage(filepath, getCompressedUriPath);
if (aDetector.isConnectingToInternet()) {
sendMediaUpload(getCompressedPath, "image");
} else {
Toast.makeText(getApplicationContext(), "Please check your internet connection", Toast.LENGTH_LONG).show();
}
// ImageAdapterList myAdapter = new ImageAdapterList();
// listview.setAdapter(myAdapter);
} else {
Cursor cursor1 = MediaStore.Video.query(getContentResolver(), selectedImage, new String[]{MediaStore.Video.VideoColumns.DURATION});
cursor1.moveToFirst();
String dur = cursor1.getString(cursor1.getColumnIndex("duration"));
int duration = Integer.parseInt(dur);
duration = duration/1000;
Log.e("dur", "dur : " + dur);
Log.e("duration", "duration : " + duration);
videofilepath = filepath;
imagefilepath = videofilepath;
sendMediaUpload(imagefilepath, "video");
// ImageAdapterList myAdapter = new ImageAdapterList();
// listview.setAdapter(myAdapter);
}
} else {
// Toast.makeText(getApplicationContext(),"Sorry! Failed to select File from gallery", Toast.LENGTH_SHORT).show();
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} else if (resultCode == RESULT_CANCELED) {
gocodercamera.setVisibility(View.VISIBLE);
gocodercamera.startPreview();
// Toast.makeText(getApplicationContext(),"User cancelled to select File", Toast.LENGTH_SHORT).show();
} else {
gocodercamera.setVisibility(View.VISIBLE);
gocodercamera.startPreview();
// Toast.makeText(getApplicationContext(),"Sorry! Failed to select File", Toast.LENGTH_SHORT).show();
}
}`
我提到在清單文件的權限提供。我設定了目標sdk 22。 –