Я получил эту ошибку при попытке поделиться изображением в Gmail с помощью FileProvider
.
Androidmanifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="staging.mytours.co"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
Файл file_provider_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="uploads" path="." />
</paths>
Код класса Java:
imageUri = FileProvider.getUriForFile(getApplicationContext(),"staging.mytours.co",new File(Uri.parse(http://staging.mytours.co/uploads/ + tour.getImageCover()).getPath()));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "TOUR:" + tour.getTitle());
shareIntent.putExtra(Intent.EXTRA_TEXT, "My Tour");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share via"));
Уже пробовали другие решения с этого сайта, но в моем случае ничего не помогло.
Обновление:
Я добавил следующую функцию для сохранения изображения в папку /data/co.mytours в хранилище.
public File DownloadFileFromURL()
{
String urlimage = http://staging.mytours.co/uploads/ + tour.getImageCover();
int count;
try {
URL url = new URL(urlimage);
URLConnection conection = url.openConnection();
conection.connect();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream(storagePath
+ "/data/co.mytours");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
return storagePath;
} catch (Exception e) {
Log.e("Error: ", "Exceptio Occured");
return null;
}
}
И назовите это,
File imagepath = DownloadFileFromURL();
Uri imageUrishare = FileProvider.getUriForFile(getApplicationContext(), "co.mytours", new File(imagepath.getPath()));
Но по-прежнему появляется ошибка. Попытка вызвать виртуальный метод java.lang.String java.io.File.getPath()
для ссылки на нулевой объект.