I have a problem in my android app while trying to create a pdf file from view of app.
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Documents/MyList/MyList.pdf: open failed: ENOENT (No such file or directory)
I had an error such like above. What am I missing there?
How can I handle it? Can you help me? Thanks.
Here is the Manifest file of my project.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.furkan.tercihrehberi">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<!--activities...-->
<activity
android:name=".ListeMyo"
android:label="#string/app_name">
</activity>
<activity
android:name=".PDFGoster"
android:label="#string/app_name">
<intent-filter >
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".PDFGosterTeog"
android:label="#string/app_name">
<intent-filter >
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
ImageButton save = (ImageButton) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
Toast.makeText(PDFGoster.this,"You cannot do that.",Toast.LENGTH_LONG).show();
}
File pdfDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
"MyList");
if (!pdfDir.exists()){
pdfDir.mkdir();
}
LayoutInflater inflater = (LayoutInflater) PDFGoster.this.getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.pdf, null);
root.setDrawingCacheEnabled(true);
Bitmap screen= getBitmapFromView(PDFGoster.this.getWindow().findViewById(R.id.tabla_cuerpo));
File pdfFile = new File(pdfDir, "MyList.pdf");
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document,byteArray);
document.close();
}
catch (Exception e){
e.printStackTrace();
}
Intent intent = new Intent("android.intent.action.OPEN_DOCUMENT");
Uri uri = Uri.fromFile(new File(pdfDir, "MyList"));
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Toast.makeText(PDFGoster.this, "List successfully created", Toast.LENGTH_LONG).show();
startActivity(intent);
}
});
Related
When I click the share button, it opens and shows some apps, but I click one app, for eg, WhatsApp it open and I choose the contact but when clicking the send button it shows "file can't be send". where I made a mistake, please help me to solve this code.
here I attached code for the share button.
//get an image from imageview
Drawable drawable = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
//sharing image
try {
File file = new File(MainActivity.this.getExternalCacheDir(), "myImage.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
//sharing intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share via"));
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "File not Found", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
Try adding
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
here is my android manifest file,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.photoeditor">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android-hardware.camera"
android:required="false" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationID}.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_path"
tools:replace="android:resource"/>
</provider>
</application>
I'm trying to read a file from sd but I can't.
This is my code.
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyMaterialTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
My class for read file:
public ParsingFromTxt(String nomeFile, ArrayList<Squadra> squadre) throws IOException
{
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard, "07-01.txt");
if(file.exists())
Log.d("log", "esiste");
Log.d("log", file.getAbsolutePath());
try
{
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
Log.d("log", br.readLine());
while ((line = br.readLine()) != null)
{ ...
}
}
catch(Exception exc)
{
Log.d("log", "errore");
}
This is what I read in the console:
11-05 16:32:25.980 2144-2144/prova.myapplication D/log: esiste
11-05 16:32:25.980 2144-2144/prova.myapplication D/log: /storage/12EA-101E/07-01.txt
11-05 16:32:25.980 2144-2144/prova.myapplication D/log: Errore
Help me to find the error.
I think you need to add one more permission to manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Does it work on a real device?
Do you have <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> permission in your AndroidManifest.xml?
Yesterday i post this question and some one replied me by peace of codes but the problem is permission denied. i do not know why happen this error because i have allowed permission in my manifest.xml. below is peace of code.
This is java code
public void WriteText() {
EditText txt= (EditText) findViewById(R.id.txtwrite);
try {
BufferedWriter fos = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+"File.txt"));
fos.write(txt.getText().toString().trim());
fos.close();
Toast.makeText(this, "Saved", Toast.LENGTH_LONG);
} catch (Exception e) {
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
}
}
This is manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Please any one help me i was so Straggled about it.
This function is used to save image on sdcard (I have tested it on the emulator only):
public String SaveImage(String URL,String imagename){
Bitmap bitmap = null;
InputStream in = null;
String path = Environment.getExternalStorageDirectory().toString();
File dir = new File(path + "/bh");
if(!dir.exists())
new File(path + "/bh").mkdir();
Log.i("in save()", "after file");
File mImageFile = new File(path+"/bh/"+imagename);
if(mImageFile.exists())
Toast.makeText(mContext, "Saved", Toast.LENGTH_LONG).show();
try{
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
FileOutputStream out = new FileOutputStream(mImageFile);
bitmap.compress(CompressFormat.JPEG, 100,out);
out.flush();
out.close();
in.close();
return imagename+".jpg";
}catch(IOException ex){
Log.e("==== Error in saving image ====",ex.getMessage());
return "";
}
}
I have added the required permissions like below :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobile.bh"
android:versionCode="4"
android:versionName="1.0.3" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name="mobile.bh.activities.BHActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.RecipesListActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.RecipeInfoActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.IngredientsActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.MethodActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.SpicesListActivity"
android:screenOrientation="portrait" >
</activity>
<activity android:name=".activities.SpicesCategoriesActivity" >
</activity>
<activity android:name=".activities.CategoryActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You also need to use the Internet permission since you're connecting to the internet.
<uses-permission android:name="android.permission.INTERNET" />
Does your emulator support SDCard ?
Have a look at this great answer, it might be the issue https://stackoverflow.com/a/11468278/1635817
I try to save some data in internal storage on android but I keep getting a NullPointerException I think it has to do with the getFilesDir() I'm using but I'm not sure. Can Some Please help clarify if that is that case and help me write this file to the device. Here the Error message im am getting
01-20 22:11:59.020: E/AndroidRuntime(329): FATAL EXCEPTION: main
01-20 22:11:59.020: E/AndroidRuntime(329): java.lang.NullPointerException
01-20 22:11:59.020: E/AndroidRuntime(329): at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
01-20 22:11:59.020: E/AndroidRuntime(329): at yantz.imageapp4.main.writeElement(main.java:89)
01-20 22:11:59.020: E/AndroidRuntime(329): at yantz.imageapp4.Panel.onTouchEvent(Panel.java:102)
Here is the oncreate of the main class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout sv = new FrameLayout(this);
LinearLayout ll = new LinearLayout(this);
test = new Panel(this);
test.settest(1) ;
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(test);
sv.addView(ll);
setContentView(sv);}
Here my OnTouch method in the panel class
public boolean onTouchEvent(MotionEvent event) {
mainactivity=new main();
mainactivity.writeElement(new Element(getResources(),(int) event.getX(),(int) event.getY()));
Log.v("Gesture", "is 1 ");
return super.onTouchEvent(event);
}
Here is my writeObject method inside my main class
public void writeElement(Element obj){
Log.v("main", "made it to method writeElement" );
File f = new File(getFilesDir()+FILENAME);
try {
fos = new FileOutputStream(f);
ObjectOutputStream objectwrite = new ObjectOutputStream(fos);
objectwrite.writeObject(obj);
fos.close();
Log.v("main", "file was made File ");
}catch (FileNotFoundException e){
e.printStackTrace();
Log.v("main", "file was not made File not found ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("main", "file was not made File IOException ");
}
}
manifest
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name="yantz.imageapp4.main" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".charactors" android:label="#string/app_name"
android:theme="#android:style/Theme.Black">
<intent-filter>
<action android:name="yantz.imageapp4.charactors" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".main2" android:label="#string/app_name"
android:theme="#android:style/Theme.Black">
<intent-filter>
<action android:name="yantz.imageapp4.main2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I know what's wrong. You can't just write
mainactivity=new main();
getFilesDir should get the correct context instance but it doesn't now.
Try something like that:
public boolean onTouchEvent(MotionEvent event) {
main.writeElement(new Element(...), this.getContext);
return super.onTouchEvent(event);
}
public static void writeElement(Element obj, Context context){
...
File f = new File(context.getFilesDir(), FILENAME);
...
}
Please try,
Open your AndroidManifest.xml and write following lines after the closing of tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>