I want to use a pre fill database in my app so I found this code.
DB_NAME = "urg1718.sqlite"
public SQLiteDatabase openDatabase() {
File dbFile = context.getDatabasePath(DB_NAME);
String path = dbFile.getAbsolutePath();
Log.i("path", "openDatabase: " + path);
if (!dbFile.exists()) {
try {
//the file doesn't exist yet so this function is called
copyDatabase(dbFile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}
//check if the database is present in the folder
Log.i("database", "openDatabase: " + dbFile.exists());
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}
private void copyDatabase(File dbFile) throws IOException {
//pre fil database
InputStream is = context.getAssets().open(DB_NAME);
try {
//where the error occurs
OutputStream os = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
os.flush();
os.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();;
} catch (IOException e) {
e.printStackTrace();
}
}
The "OutputStream os = new FileOutputStream" catch the following error.
java.io.FileNotFoundException: /data/data/jle.urgdegarde17_18/databases/urg1718: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
W/System.err: at java.io.FileOutputStream.(FileOutputStream.java:87)
W/System.err: at java.io.FileOutputStream.(FileOutputStream.java:127)
W/System.err: at java.io.FileOutputStream.(FileOutputStream.java:116)
W/System.err: at jle.urgdegarde17_18.AssetDatabaseOpenHelper.copyDatabase(AssetDatabaseOpenHelper.java:49)
W/System.err: at jle.urgdegarde17_18.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:35)
W/System.err: at jle.urgdegarde17_18.Chargement.isStoragePermissionGranted(Chargement.java:40)
W/System.err: at jle.urgdegarde17_18.Chargement.onCreate(Chargement.java:18)
W/System.err: at android.app.Activity.performCreate(Activity.java:5990)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:151)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:135)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
W/System.err: ... 20 more
I tried to use getDatabasePath(), getDatabasePath().getAbsolutePath() and directly put the string returned by those functions into FileOutputStream
I also tried to remove the ".sqlite" from the database name.
I don't understand what i'm doing wrong ...
Thank's.
Related
I am trying to create a image file to external storage to share it. but while trying following code I am having some error
var icon: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.namelogo)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/*"
val bytes = ByteArrayOutputStream()
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
val path = File.separator + "temporary_file.jpg"
val f = File(Environment.getExternalStorageDirectory().toString(), path)
try {
if (!f.parentFile.exists())
f.parentFile.mkdirs()
if (!f.exists())
f.createNewFile()
f.createNewFile()
val fo = FileOutputStream(f)
fo.write(bytes.toByteArray())
} catch (e: IOException) {
Log.e("path = ", "+ $path " + e.toString())
e.printStackTrace()
}
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/IronyBalls/temporary_file.jpg"));
startActivity(Intent.createChooser(shareIntent, "Share Image"))
Till now I found solutions only to use
if (!f.parentFile.exists())
f.parentFile.mkdirs()
if (!f.exists())
f.createNewFile()
and
to set permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
that I have already used.But still gettig error as below
in Error Tab
E/path =: + /temporary_file.jpg java.io.IOException: Permission denied
and in info tab
W/System.err: java.io.IOException: Permission denied
W/System.err: at
java.io.UnixFileSystem.createFileExclusively0(Native Method)
W/System.err: at
java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
W/System.err: at java.io.File.createNewFile(File.java:948)
W/System.err: at
com.irony.balls.MainActivity$onCreate$8.onClick(MainActivity.kt:277)
W/System.err: at android.view.View.performClick(View.java:5611)
10-15 20:33:17.912 29759-29759/com.irony.balls
W/System.err: at
android.view.View$PerformClick.run(View.java:22276) 10-15 20:33:17.912
29759-29759/com.irony.balls
W/System.err: at
android.os.Handler.handleCallback(Handler.java:751) 10-15 20:33:17.912
29759-29759/com.irony.balls
W/System.err: at
android.os.Handler.dispatchMessage(Handler.java:95) 10-15 20:33:17.912
29759-29759/com.irony.balls
W/System.err: at
android.os.Looper.loop(Looper.java:154) 10-15 20:33:17.912
29759-29759/com.irony.balls W/System.err: at
android.app.ActivityThread.main(ActivityThread.java:6195) 10-15
20:33:17.912 29759-29759/com.irony.balls W/System.err: at
java.lang.reflect.Method.invoke(Native Method) 10-15 20:33:17.912
29759-29759/com.irony.balls W/System.err: at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
10-15 20:33:17.912 29759-29759/com.irony.balls W/System.err: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
If your device is working on Android 6 or higher have you already requested runtime permissions?
See https://developer.android.com/training/permissions/requesting.html for more details.
I am opening a twitter stream to get a constant stream of tweets, but when I attempt to close it I get the following system errors. The errors occur when the method stopStreaming() is called. I am using the Twitter twitter4j for streaming a statuses/filter.
public class FilterTweetsManager implements StatusListener//, Runnable
{
//
public void start(String[] keywords)
{
FilterQuery fq = new FilterQuery();
fq.track(keywords);
twitterStream_.addListener(this);
twitterStream_.filter(fq);
}
public void stopStreaming()
{
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
twitterStream_.cleanUp(); // shutdown internal stream consuming thread
twitterStream_.shutdown(); // Shuts down internal dispatcher thread shared by all TwitterStream instances.
}
});
}
#Override
public void onStatus(twitter4j.Status status) {
Log.d(TAG, "onStatus " + status.getText());
}
//... rest of the StatusListener methods
}
The errors received are:
W/System.err: java.lang.IllegalStateException: Unbalanced enter/exit
W/System.err: at com.android.okhttp.okio.AsyncTimeout.enter(AsyncTimeout.java:62)
W/System.err: at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:209)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
W/System.err: at com.android.okhttp.internal.http.Http1xStream$ChunkedSource.readChunkSize(Http1xStream.java:451)
W/System.err: at com.android.okhttp.internal.http.Http1xStream$ChunkedSource.read(Http1xStream.java:435)
W/System.err: at com.android.okhttp.internal.Util.skipAll(Util.java:159)
W/System.err: at com.android.okhttp.internal.Util.discard(Util.java:141)
W/System.err: at com.android.okhttp.internal.http.Http1xStream$ChunkedSource.close(Http1xStream.java:472)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.close(RealBufferedSource.java:396)
W/System.err: at com.android.okhttp.okio.RealBufferedSource$1.close(RealBufferedSource.java:384)
W/System.err: at java.util.zip.InflaterInputStream.close(InflaterInputStream.java:239)
W/System.err: at java.util.zip.GZIPInputStream.close(GZIPInputStream.java:137)
W/System.err: at twitter4j.StatusStreamBase.close(StatusStreamBase.java:335)
W/System.err: at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.close(TwitterStreamImpl.java:685)
W/System.err: at twitter4j.TwitterStreamImpl.cleanUp(TwitterStreamImpl.java:386)
W/System.err: at cat.jorda.tweetfilter.FilterTweetsManager$1.run(FilterTweetsManager.java:63)
W/System.err: at android.os.Handler.handleCallback(Handler.java:789)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98)
W/System.err: at android.os.Looper.loop(Looper.java:164)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
W/TwitterStreamImpl: Unbalanced enter/exit
W/TwitterStreamImpl: Inflater has been closed
when I try to get the input stream from an URL, I get the following Error:
W/System.err: java.io.FileNotFoundException: https://...
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
W/System.err: at com.andrei.mobilissimoro.MainActivity$FeedTask.doInBackground(MainActivity.java:406)
W/System.err: at com.andrei.mobilissimoro.MainActivity$FeedTask.doInBackground(MainActivity.java:371)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err: at java.lang.Thread.run(Thread.java:761)
And the response code is 500.
Here is the code:
HttpURLConnection httpURLConnection;
try {
httpURLConnection = (HttpURLConnection)new URL(feed).openConnection();
httpURLConnection.setRequestMethod("GET");
Integer status = httpURLConnection.getResponseCode();
System.out.println(status.toString());
InputStream inputStream = httpURLConnection.getInputStream();
feedModelList = parseFeed(inputStream);
}
catch (Exception e){
e.printStackTrace();
}
Server will send 500 code, when there is some exception while executing code on server while responding to your API call.
I'm trying to create a file in the external storage directory of the Nougat emulator. using the following code:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test987689");
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
Toast.makeText(this, "External SD card not mounted", Toast.LENGTH_LONG).show();
}try {
if (file.getParentFile().exists() || file.getParentFile().mkdirs()) {
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
Permissons:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And I'm getting the following error:
W/System.err: java.io.IOException: Permission denied
W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
W/System.err: at java.io.File.createNewFile(File.java:958)
W/System.err: at com.vibhinna.sreni.SplashActivity.onCreate(SplashActivity.java:41)
W/System.err: at android.app.Activity.performCreate(Activity.java:6658)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2692)
W/System.err: at android.app.ActivityThread.-wrap12(ActivityThread.java)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6044)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
What am I doing wrong?
If your targetSdk is 23 or higher, you should request permissions dynamically.
to know more : Requesting Permissions at Run Time
to get File path you can use Context.getExternalFilesDir()/Context.getExternalCacheDir()
for example
String path=Context.getExternalCacheDir()+"file.text";
File file=new File(path)
it doesnt need permission if the filepath is "Android/data/app package/file name"
This is my code where i'm trying to access a file from /data directory on my rooted device. My app is successfully getting root privileges and also, it can access data from other folders like / or /sdcard or /system etc. The problem arises only in the case of /data directory. And, i'm sure such file exists on the file system and getting the correct path. If the file doesn't exist, then it shows a different error. Now, how can i read the file inside /data directory?
try {
File wifiConfFile = new File(Environment.getDataDirectory()+ File.separator + "system" + File.separator + "appops.xml");
if(!wifiConfFile.exists()){
Log.d("FILE", Environment.getDataDirectory()+"/system/appops.xml couldn't be loaded!");
}
fis = new FileInputStream(wifiConfFile);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
wifiDesc.append(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This is the permission block in my android-menifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
And, this is from android monitor...
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: java.io.FileNotFoundException: /data/system/appops.xml: open failed: EACCES (Permission denied)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at rizanamic.wifipass.MainActivity.onCreate(MainActivity.java:71)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.app.Activity.performCreate(Activity.java:5933)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.os.Looper.loop(Looper.java:135)
12-08 19:47:28.326 26634-26634/rizanamic.wifipass W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5229)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: at libcore.io.Posix.open(Native Method)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
12-08 19:47:28.327 26634-26634/rizanamic.wifipass W/System.err: ... 15 more