I have successfully imported the library "com.ipaulpro.afilechooser.utils.FileUtils" to export and import the database on the SD card. But in class I get error on line:FileUtils.copyFile(internalDB, externalDB);
File internalDB = new File("/data/data/"+getPackageName()+"/databases/MyDatabase.db");
File externalDB = new File(Environment.getExternalStorageDirectory(), getPackageName()+"/database/MyDatabase.db");
try {
FileUtils.copyFile(internalDB, externalDB);
Toast toast = Toast.makeText(getApplicationContext(),(R.string.Toast_export), Toast.LENGTH_SHORT);
//toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();
} catch (IOException e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(),(R.string.Toast_export_errore), Toast.LENGTH_SHORT);
toast.show();
}
Related
I continually get an error.
Each time that I run the code in the emulator, it shows the 'Toast' that the directory was created, but there must be an error shortly after that line of code. The error that comes up is:
"/storage/sdcard/Pictures/screenshot.png: open failed: ENOENT (No such file or directory)"
I have place the relevant code below.
<manifest
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19"
/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"
/>
</manifest>
public class myActivity {
private void openScreenPrint() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)){
View v1 = findViewById(R.id.myRelativeLayout).getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap myBM = Bitmap.createBitmap(v1.getDrawingCache());
saveBitmap(myBM);
v1.setDrawingCacheEnabled(false);
}
else{
Toast.makeText(this, "No Permission to Write", Toast.LENGTH_SHORT).show();
}
}
public void saveBitmap(Bitmap bitmap) {
FileOutputStream fos = null;
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File dir = new File(filePath);
if (!dir.exists()){
dir.mkdirs();
Toast.makeText(this, "created", Toast.LENGTH_LONG).show(); //This line shown every time
}
String fileName = "screenshot" + ".png";
File imagePath = new File(filePath, fileName);
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
//Log.e("Err", e.getMessage(), e);
} catch (IOException e) {
//Log.e("Err", e.getMessage(), e);
}
}
}
Have you created an SD Card in your AVD? If not, here's the entry that you'll want to use when creating/editing your simulated device:
You do not actually know that the directory is actually being created. You are making the Toast whether the directory create succeeds or not. You should change this section:
if (!dir.exists()){
dir.mkdirs();
Toast.makeText(this, "created", Toast.LENGTH_LONG).show();
}
to test if the directory was actually created successfully. File.mkdirs() returns a boolean.
if (!dir.exists()) {
Toast.makeText(this, "dir not exists. attempting to create...", Toast.LENGTH_SHORT).show();
if (dir.mkdirs()) {
Toast.makeText(this, "dir created", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "dir creation failed", Toast.LENGTH_SHORT).show();
}
}
I have the following code to share a file.
It all seems to work fine,
Gmail opens with the attachment attached,
but when I click SEND Gmail stops and gives an error Unfortunately Gmail has stopped
The same with Google Drive, all seems to work fine but in the end I get:
Upload failed by Google drive.
Any help very much appreciated!!!
share.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fileuri=Utility.downloadDb(MainActivity.this);
if(fileuri!=null){
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileuri);
startActivity(sharingIntent);
}
}
});
Where Utility.downloadDb(MainActivity.this);
is:
public static Uri downloadDb(Context context) {
DatabaseHandler db= new DatabaseHandler(context);
ArrayList<Word> list=new ArrayList<Word>();
Uri fileuri=null;
list.clear();
list.addAll(db.getAllWords());
File path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "db.csv");
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
// Make sure the Pictures directory exists.
path.mkdirs();
FileWriter outFile = new FileWriter(file);
PrintWriter out = new PrintWriter(outFile);
out.print("PICTURE NAME");
out.print(",");
out.print("WORD NUMBER");
out.print(",");
out.print("LEFT");
out.print(",");
out.print("TOP");
out.print(",");
out.print("RIGHT");
out.print(",");
out.println("BOTTOM");
for(int i=0;i<list.size();i++){
out.print(list.get(i).pic);
out.print(",");
out.print(Integer.toString(list.get(i).wordno));
out.print(",");
out.print(Integer.toString(list.get(i).beginx));
out.print(",");
out.print(Integer.toString(list.get(i).beginy));
out.print(",");
out.print(Integer.toString(list.get(i).endx));
out.print(",");
out.println(Integer.toString(list.get(i).endy));
}
out.close();
Toast.makeText(context, "SAVED TO: " +file.getAbsolutePath(), Toast.LENGTH_LONG).show();
fileuri=Uri.parse(file.getAbsolutePath());
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.e("ExternalStorage", "Error writing " + file, e);
}
return fileuri;
}
Solution found!!!!
I think this might be useful for other people as well!!!
to get the URI use:
fileuri=Uri.fromFile(file);
NOT
fileuri=Uri.parse(file.getAbsolutePath());
i m trying to create directory in sd card through this card.but it is not worked.i put write external storage permission in manifest though it is not working.need urgent help.i had put this code in try catch.but it doesn't enter in catch block.here is my code..
try
{
File songDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");
if(!songDirectory.exists())
{
songDirectory.mkdirs();
Toast.makeText(getApplicationContext(),"Directory created", Toast.LENGTH_LONG).show();
// ShowlistView();
}
else
{
//ShowlistView();
Toast.makeText(getApplicationContext(),"Directory AlreadyExists", Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("","Error While creating file is:::::"+e+"");
}
Try out the Below Code :
File sdDir = Environment.getExternalStorageDirectory();
File wwwjdicDir = new File(sdDir.getAbsolutePath() + "/your_folder_name");
if (!wwwjdicDir.exists()) {
wwwjdicDir.mkdir();
}
if (!wwwjdicDir.canWrite()) {
return;
}
try like
File dir = new File(DEFAULT_STORAGE_LOCATION);
// test dir for existence and writeability
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (Exception e) {
return null;
}
} else {
if (!dir.canWrite()) {
return null;
// do your work here
}
}
If this is your code
File songDirectory = new `File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");
you need to chagne it to this
File songDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");
I'm taking a screenshot from a layout that have a videoview, I have this code to save it, and to capture it, but when I open it I have a blank image but not an empty the image have 1.5Kb, can you help me?
this is how I capture the screenshot
public void TakePic(View v){
buton = AnimationUtils.loadAnimation(this, R.anim.but);
v.startAnimation(buton);
if (vid!=null)
{
if(vid.getCurrentPosition()!=0)
{
popupc = (LinearLayout) findViewById(R.id.guardapic);
popupc.setVisibility(View.VISIBLE);
LinearLayout layout = (LinearLayout)findViewById(R.id.videopic);
layout.setDrawingCacheEnabled(true);
layout.setDrawingCacheQuality(LinearLayout.DRAWING_CACHE_QUALITY_HIGH);
layout.buildDrawingCache();
bitmap = layout.getDrawingCache();
im=(ImageView)findViewById(R.id.imgdown);
// im.setImageResource(R.drawable.play_amarelo);
im.setImageBitmap(bitmap);
}
else
{
Toast toast = Toast.makeText(ctx,"Video has stopped...Restart", Toast.LENGTH_SHORT);
toast.show();
}
}
else
{
Toast toast = Toast.makeText(ctx,"Start video first", Toast.LENGTH_SHORT);
toast.show();
}
}
this is the code to save it into the sdCard
public void PicOk(View v){
String pathpic=null;
String nomepic=null;
EditText path= (EditText)findViewById(R.id.picpath);
EditText pic= (EditText)findViewById(R.id.nomepic);
pathpic=path.getText().toString();
nomepic=pic.getText().toString();
File folder = new File(Environment.getExternalStorageDirectory() + "/"+pathpic);
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (!success) {
Log.d("Lino"," Pasta nao criada");
} else {
FileOutputStream ostream;
try {
File file = new File(folder.toString() + "/"+nomepic+ ".png");
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 95, ostream);
ostream.flush();
ostream.close();
// ((LinearLayout)findViewById(R.id.VV2)).destroyDrawingCache();
} catch (FileNotFoundException e) {
Log.d("Lino","erro"+e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d("lino","erro "+e.toString());
e.printStackTrace();
}
}
popupc = (LinearLayout) findViewById(R.id.guardapic);
popupc.setVisibility(View.GONE);
bitmap=null;
//tira foto
Toast toast = Toast.makeText(ctx,"pick taked", Toast.LENGTH_SHORT);
toast.show();
}
You know the video is actually Combination of Still Images. The moment when you take the picture the cross-ponding frame is blank. That is why screenshot appears to be black/blank.
So with this method you can't take the screen shot of a video. You need to adopt a different approach.
May be it helps you.
I have a test class that extends ProviderTestCase2<>.
I would like to populate this test class database with data from some .db files.
Is there some particular method to push some .db file into the Mock Context of a ProviderTestCase2?
Otherwise which way is the easier to populate the database from the .db file?!
Thank you very much!!
How about copying in a pre-existing .db file from the SD Card or something similar? This is a quick piece of code that will accomplish this for you:
private void importDBFile(File importDB) {
String dataDir = Environment.getDataDirectory().getPath();
String packageName = getPackageName();
File importDir = new File(dataDir + "/data/" + packageName + "/databases/");
if (!importDir.exists()) {
Toast.makeText(this, "There was a problem importing the Database", Toast.LENGTH_SHORT).show();
return;
}
File importFile = new File(importDir.getPath() + "/" + importDB.getName());
try {
importFile.createNewFile();
copyDB(importDB, importFile);
Toast.makeText(this, "Import Successful", Toast.LENGTH_SHORT).show();
} catch (IOException ex) {
Toast.makeText(this, "There was a problem importing the Database", Toast.LENGTH_SHORT).show();
}
}
private void copyDB(File from, File to) throws IOException {
FileChannel inChannel = new FileInputStream(from).getChannel();
FileChannel outChannel = new FileOutputStream(to).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
Hopefully this will work for your scenario