I tried to save "viewBitmap" into SD card.
Here is my code:
try{
String mPath = Environment.getExternalStorageDirectory().toString();
imageFile = new File(mPath, "/snapshot.png");
FileOutputStream outputStream = new FileOutputStream(imageFile);
viewBitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
outputStream.flush();
outputStream.close();
Toast.makeText(Share.this, "Collage was saved.",Toast.LENGTH_SHORT).show();
}catch(Throwable e) {
e.printStackTrace();
}
At the line "FileOutputStream outputStream = new FileOutputStream(imageFile);", an exception was thrown:
In your AndroidManifest.XML file did you add permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, refer to the documentation to request the permission at run time.
Related
I am new to android . I would like to store and retrieve records in android studio . I am using embedded sql database in android studio . Data are inserted perfectly when my device is connected to studio. whenever, i run after disconnecting my device insertion is not performing . what is the problem ? please help me, what should i need to do?
Try this code
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ "com.example.yourpackname" +"/databases/"+SAMPLE_DB_NAME;
String backupDBPath = SAMPLE_DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(IOException e) {
e.printStackTrace();
}
}
Add permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I have tried to implement over 10-15 different download mechanisms for android java, I have not been able to succeed at all.
I don't care about progress bars or background processes.
I just want one functional download code in fewest lines possible
and I want it to download a binary file (foreground) to the directory in the device wherever it can be accessed as
File pf = new File("filename");
if (pf.exists()) { ... }
Try this (modified from here):
try {
URL url = new URL("http://url.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
//THIS IS WHERE YOU GET THE DIRECTORY TO SAVE TO
File SDCardRoot = Environment.getExternalStorageDirectory();
//THIS IS WHERE YOU WILL SET THE FILE NAME
File file = new File(SDCardRoot,"somefile.txt");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You may also need to add permissions to access the phone directory:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
For information on accessing certain folders on the internal directory, see the android developer page: http://developer.android.com/training/basics/data-storage/files.html#WriteInternalStorage
In fact, the solution on that page is also fairly short:
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
First, I'll say that I've read many other posts that match this question and none of them have worked for me.
I'm testing my app on my device, a Nexus 5 running Android L. It has not been rooted.
This same code works on an older Android, running API 19.
I'm trying to take a screenshot and share it, using this code:
View screen = getWindow().getDecorView().getRootView();
screen.setDrawingCacheEnabled(true);
Bitmap bitmap = screen.getDrawingCache();
String filename = getScreenshotName();
String filePath = Environment.getExternalStorageDirectory().getPath()
+ File.separator + filename;
File imageFile = new File(filePath);
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bitmapData = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(bitmapData);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
// share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
startActivity(Intent.createChooser(share, "Share Image"));
I have these permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
And I get this error:
java.io.FileNotFoundException: /storage/emulated/0/2014-09-14.png: open failed: EACCES (Permission denied)
On this line:
FileOutputStream fos = new FileOutputStream(imageFile);
I've also tried 10 other ways to get a filePath, and I now suspect this is a device/Android L issue.
Any idea what's happening?
I am trying to download a jar file from my server and put it into the AVD internal memory but it's not working. I tried this code in Java and it's working perfectly.
try
{
URL url = new URL(host);
URLConnection connection = url.openConnection();
int fileLength = connection.getContentLength();
if (fileLength == -1)
{
return;
}
input = connection.getInputStream();
String fileName = url.getFile().substring(url.getFile().lastIndexOf('/') + 1);
writeFile = new FileOutputStream(fileName);
byte[] buffer = new byte[1024];
int read;
while ((read = input.read(buffer)) > 0)
writeFile.write(buffer, 0, read);
writeFile.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
writeFile.close();
input.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
I add the following permission into my manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
But on connection.getContentLength();, I got a NullPointerException return
I tried with HttpURLConnection and JarURLConnection, add a connection.connect() just after openConnection(), using DownloadManager but it can only download into external storage.
Maybe with HttpClient ? AndroidHttpClient ? But Android support recommend using HttpURLConnection for applications targeted at Gingerbread and higher.
There's a cumbersome but straightforward approach:
1. Use DownloadManager to get a JAR you need (store it in external storage);
2. Move that JAR from external storage to internal one.
I use the code below to get image:
public Bitmap loadImageFromUrl(String urlStr) {
try {
BufferedInputStream bis = new BufferedInputStream(new URL(urlStr).openStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
copy(bis, bos);
bos.flush();
bos.close();
bis.close();
return BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());
} catch (Exception e) {
e.printStackTrace();
}
I'm sure of the permisson and wifi connection.
such as:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If you are getting this error then might be there is 2 issue.
1) Either you forget to add permission of internet in android manifest.
<uses-permission android:name="android.permission.INTERNET" />
2) If you are running in real device then your device is not connected with internet. Check whether your device is connected with internet or not.