Hallo!
Ich möchte eine content URI einlesen, und das Bild in diesem Fall, an einen anderen Ort kopieren. Aber: wie bekomme ich den Pfad und Dateinamen?
Ich habe folgendes:
Das ist die URI die ich einlesen will:
Fehler:
Ich möchte eine content URI einlesen, und das Bild in diesem Fall, an einen anderen Ort kopieren. Aber: wie bekomme ich den Pfad und Dateinamen?
Ich habe folgendes:
Java:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check if photo was selected
if(resultCode == RESULT_OK) {
// if it was photo loading intent
if(requestCode==0) { // if intent is from imageview
Uri imgUri = data.getData();
...
Java:
public Uri saveCover(Context context, Uri uri) {
String oPath = Environment.getExternalStorageDirectory()+"/test/data/";
new File(oPath).mkdirs();
File iF = contentUriToFile(context, uri);
File oF = new File(oPath+iF.getName());
try {
oF.createNewFile();
InputStream fis =new FileInputStream(iF);
FileOutputStream fos =new FileOutputStream(oF);
byte[] buffer =new byte[1024];
int length;
while ((length=fis.read(buffer)) >0) {
fos.write(buffer);
}
fis.close();
fos.close();
return Uri.fromFile(oF);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public File contentUriToFile(Context context, Uri uri) {
String filePath = null;
Uri _uri = uri;
if (_uri != null && "content".equals(_uri.getScheme())) {
Cursor cursor = context.getContentResolver().query(_uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
} else {
filePath = _uri.getPath();
}
return new File(filePath);
}
Fehler:
Code:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65536, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:53173 flg=0x1 }} to activity {com.ludevstudio.mp3audiobookplayer/com.ludevstudio.mp3audiobookplayer.AddBookActivit}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:5041)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5084)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2053)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.NullPointerException
at java.io.File.<init>(File.java:282)
at com.ludevstudio.mp3audiobookplayer.data.Library.contentUriToFile(Library.java:252)
at com.ludevstudio.mp3audiobookplayer.data.Library.saveCover(Library.java:211)
at com.ludevstudio.mp3audiobookplayer.AddBookStep3BookFragment.onActivityResult(AddBookStep3BookFragment.java:164)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:160)
at android.app.Activity.dispatchActivityResult(Activity.java:7701)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5037)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5084)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2053)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
I/Process: Sending signal. PID: 2537 SIG: 9
Application terminated.