Сейчас я занимаюсь разработкой приложения для загрузки WhatsApp Statues. Я сделал, чтобы показать изображения из папки WhatsApp для моего приложения Recyclerview. Теперь я застрял при сохранении файлов изображений, исходной папки в папку назначения.
По методу клика в моем RecyclerView:
holder.mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//mDataset is contain the Image files
try {
file_operation.save(mDataset.get(ourPosition));
Toast.makeText(mContext, "saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(mContext, "saving failed", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Мой класс saveImage:
package com.example.storage;
import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class file_operation {
public static void save(File sourcefile) throws IOException {
File f3 = sourcefile;
String filename = f3.getName();
File desti_file = new File(Environment.getDataDirectory().toString() + FilesData.getSavedFilesLocation(), filename);
if (!desti_file.exists()) {
desti_file.mkdir();
}
if (!desti_file.getParentFile().exists()) {
desti_file.mkdirs();
}
if (!desti_file.exists()) {
desti_file.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(f3).getChannel();
destination = new FileOutputStream(desti_file).getChannel();
// inChannel.transferTo(0, inChannel.size(), outChannel);
source.transferTo(0, source.size(), destination);
// destination.transferFrom(source, 0, source.size());
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
FilesData.scrapSavedFiles();
}
}
Когда я запускаю приложение, я всегда получаю сообщение «Saving Failed». Я пробовал много способов, но не могу сохранить изображение.