Reputation: 192
I am trying to create a file organizer app but when created an activity to receive shared audio,video, or image it works fine but the problem that I want to copy that file from Uri to a specific folder in external data storage. I have tried a lot of code and read all the related questions, but no one solve my problem.
this is what I tried:
the permissions are
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
the activity declaration at manifest:
<activity
android:name="nahamsoft.com.Home"
android:label="@string/title_activity_home"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/vnd.google.panorama360+jpg"/>
<data android:mimeType="image/*"/>
<data android:mimeType="video/*"/>
<data android:mimeType="audio/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
the Java code to receive file is:
if(Intent.ACTION_SEND.equals(action)&& type!=null){
fab.show();
Toast.makeText(this,"send action",Toast.LENGTH_LONG).show();
if("text/plain".equals(type)){
handleSentText(intent);
}else if(type.startsWith("image/")){
try {
handleSentImage(intent);
} catch (IOException e) {
e.printStackTrace();
}
}else if(type.startsWith("video/")){
handleSentVideo(intent);
}else if(type.startsWith("audio/")){
handleSentAudio(intent);
}
}
else{
Toast.makeText(this,"No data were come...",Toast.LENGTH_LONG).show();
}
the methods to copy the file are:
private void handleSentAudio(Intent intent) {
Uri audio=(Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
Toast.makeText(this,"audio were handled",Toast.LENGTH_LONG).show();
}
private void handleSentVideo(Intent intent) {
Toast.makeText(this,"Video were handled",Toast.LENGTH_LONG).show();
}
private void handleSentImage(Intent intent) throws IOException {
Uri imageUri=(Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
/*try {
MoveFile(imageUri.getPath(),"/sdcard/Alarms/");
} catch (IOException e) {
e.printStackTrace();
}*/
if(imageUri!=null){
Toast.makeText(this,"from:"+imageUri.getPath()+"\n to :"+
Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/",Toast.LENGTH_LONG).show();
File src=new File(imageUri.toString());
File des=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/");
copyFile(src,des);
}else{
Toast.makeText(this,"Image was not handled",Toast.LENGTH_LONG).show();
}
}
private void handleSentText(Intent intent) {
String sharedText=intent.getStringExtra(Intent.EXTRA_TEXT);
Toast.makeText(this,"Text is come...",Toast.LENGTH_LONG).show();
}
copy file method
public static void copyFile(File src, File dst) throws IOException
{
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try
{
inChannel.transferTo(0, inChannel.size(), outChannel);
}
finally
{
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
what I want is to move the handled file to specific folder which called myFolder.
Upvotes: 2
Views: 4345
Reputation: 1007276
Replace:
File src=new File(imageUri.toString());
with:
InputStream src=getContentResolver().openInputStream(imageUri);
Then:
Modify copyFile(src,des)
to copy an InputStream
to your destination
Eventually move this I/O to a background thread, so you do not freeze your UI for the duration of the copy operation
Upvotes: 4