1
私はsdcardから写真を撮ろうとしていますが、ユーザーがGoogleフォトから選択してもギャラリーのようなアプリから選択するとcurserindexoutofboundエラーまたはファイルマネージャ。ここにコードがあります。Googleフォトから写真をアップロードしますが、ギャラリーやファイルマネージャーから写真をアップロードしないでください
OnActivity結果 ' {IF(requestCode == PICK_IMAGE_REQUEST & &のresultCode == Activity.RESULT_OK & &ヌルデータ= & & data.getData()= NULL!) {ウリURI = NULL; 文字列realPath = null;
try
{
uri = data.getData();
Log.e("uri",uri.toString());
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
String imagePath = getRealPathFromURI(uri);
File source = new File(imagePath);
String destinationPath = Environment.getExternalStorageDirectory().toString() + "/IASFolders/"+Configuration.empcode+".jpg";
File destination = new File(destinationPath);
try
{
InputStream in = new FileInputStream(source.getAbsolutePath());
OutputStream out = new FileOutputStream(destination.getAbsolutePath());
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
Log.d("File Copy Exception", e.toString());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
`{getRealPathFromURI機能
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getActivity().getContentResolver()
.query(contentURI,new String[]{MediaStore.Images.Media.DATA},MediaStore.Images.Media.DISPLAY_NAME+"=?" ,new String[]{Configuration.empcode},null);
/*
* Cursor imageCursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[]{MediaStore.Images.Media.DATA},MediaStore.Images.Media.DISPLAY_NAME+"=?" ,new String[]{imageTitle},null);
imageCursor.moveToFirst();
String imageData=imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Long imageSize=imageCursor.getLong(imageCursor.getColumnIndex(ImageColumns.SIZE));
Toast.makeText(getApplicationContext(), String.valueOf(imageSize), Toast.LENGTH_LONG).show();*/
if (cursor == null)
{
result = contentURI.getPath();
}
else
{
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
}`
のようにこのコードを試してみてください取り組んでいますが、私のコードを見れば私はされますイメージを取得して別のイメージにコピーしようとするフォルダには例外があります。ありがとうございます。 –
@MithileshIzardar http://stackoverflow.com/questions/8664440/how-to-copy-image-file-from-gallery-to-another-folder-programatically-in-android ..選択された画像パス –
@MilesilesIzardarまたはこのhttp://stackoverflow.com/questions/4921183/android-copy-image-from-gallery-folder-onto-sd-card-alternative-folderのさらなるコピーについては、これを参考にしてください –