I'm using the class FileUtils and library ipaulpro.afilechooser.
I fixed my code but I get "copyFile" underlined in red. Will you help me understand why? Thanks
private void import(File from){
File to = new File("/data/data/"+getPackageName()+"/databases/cio.db");
try {
FileUtils.copyFile(from, to);
Toast.makeText(this, getString(R.string.Toast_o) , Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, getString(R.string.Toast_to) , Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == this.FILE_CHOOSE){
if (resultCode == RESULT_OK){
// The URI of the selected file
Uri uri = data.getData();
// Create a File from this Uri
File file = com.ipaulpro.afilechooser.utils.FileUtils.getFile(uri);
importDB(file);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Because there is no method called copyFile in https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
FileUtils of ipaulpro.afilechooser.
The copyFile method is available in the android.os.FileUtil class, but it looks like you are using ipaulpro.afilechooser.
If you want to use the android.os change your code to
android.os.FileUtils.copyFile(from, to);
Although personally I would find it a bit confusing and maybe unnecessary to use both versions of this class.
Related
Issue:
Office is a folder in internal memory of Android.
Clicking a button in screen should always take one to default folder, Office.
Appreciate help as no accepted answers found.
In onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null)
return;
if (requestCode == viewfilerequestcode){
Uri Fileuri = data.getData();
String DFLocation="/mnt/sdcard/Office/";
String FilePath="";
if (FilePath.trim().equals(DFLocation.concat(GettheFilename(Fileuri))))
{
FilePath = DFLocation.concat(GettheFilename(Fileuri));
}
......
.......
........
}
.........
..........
}
Intent start:
btnview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent fileintent;
String[] mimestoview =
{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx };
fileintent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
fileintent.setType("*/*");
fileintent.putExtra(Intent.EXTRA_MIME_TYPES, mimestoview );
fileintent.addCategory(Intent.CATEGORY_OPENABLE);
fileintent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
fileintent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivityForResult(fileintent, viewfilerequestcode);
} catch (ActivityNotFoundException e) {
lbl.setText("No activity to handle picking a file. Showing alternatives.");
}
}
});
I am having two different classes. One class is for adding image and uploading it to server and another class for editing the saved image with new image. So for getting image from sdcard, in add class I am using startactivityforresult and onactivityresult. Same thing I am trying to use in edit class because in this class im having an ImageView where new image is added but it is not allowing me to create onactivityresult in edit class. Saying error as onactivityresult is already defined. If someone knows please help me.
code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageToUpload.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Use it like this:
startActivityForResult (intent, int requestCode);
Then in onActivityResult you can use switch case using requestCode.
Like:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
//Have your cases here
}
}
I'm switching from using the default camera to finally growing a pair and deciding to make a custom camera. It's only showing me how much I don't fully understand.
Here is basically way I have been doing things in the main activity as far as photos go, but it will no longer work:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_PHOTO_REQUEST) {
if (data == null) {
Toast.makeText(this, "General Error!", Toast.LENGTH_LONG).show();
}
else {
mMediaUri = data.getData();
}
Log.i(TAG, "Media URI: " + mMediaUri);
}
else {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mMediaUri);
sendBroadcast(mediaScanIntent);
}
(...)
Here is the picture saving function along with a couple others of the new camera:
(...)
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Picture taken");
String path = savePictureToFileSystem(data);
setResult(path);
finish();
}
private static String savePictureToFileSystem(byte[] data) {
File file = getOutputMediaFile();
saveToFile(data, file);
return file.getAbsolutePath();
}
private void setResult(String path) {
Intent intent = new Intent();
intent.putExtra(EXTRA_IMAGE_PATH, path);
setResult(RESULT_OK, intent);
}
*Credit to Paul Blundell
(...)
What do I need to do so that the main activity can receive the image's URI in the onactivityresult instead of the path String? Are URIs even applicable when it comes to custom cameras?
Please and thanks.
You use custom camera, but want to do it via Intent? OK, you have the absolute file path in
String abspath = data.getExtras().getString(EXTRA_IMAGE_PATH);
mMediaUri = Uri.fromFile(new File(abspath));
I'm trying to write an Android app which automatically uploads a picture to a server, but I am stuck on just one line of code:
File f = File(context.getCacheDir(), "filename");
The error I get is
This puzzles me because I see so many examples on the web of people using context.getCacheDir() just fine, whereas I get the error message.
It's probably something wrong with my IDE settings. I am using IntelliJ IDE.
Here's is the context of the context problem:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(context.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
You need to do some basic Java programming tutorials. Java is totally different to JavaScript.
Here, you use context as a variable but you have neither declared it, or initialised it, hence the error.
You could define it (and initialise at the same time)
Context context = this;
since this refers to the current object instance of a class and Activity is a Context, or more precisely, it extends Context.
Alternatively, you could just use this.
File f = File(UploadToServer.this.getCacheDir(), "filename");
The error is bacuse you havent declared context, neither it has been passed as a parameter
change context.getCacheDir() to getApplicationContext.getCacheDir() or this.getCacheDir()
so
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(context.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
will become
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(getApplicationContext.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Add this :
Context context = this;
You need this to initialize context
Then Alt+Enter
You must assign context to this Activity
I am trying to capture camera photo in may app...
this is what I have:
The photo is saved but on the on Activity Result, I get Null point Exception.
What could I be possible missing out?
private Uri getImgUri() {
File filePath= new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES),APP_ALIAS);
if(!filePath.exists()){
if(!filePath.mkdirs())
return null;
}
String timeStamp= new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String path=filePath.getPath()+File.separator+"_IMG"+timeStamp+".jpg";
File file=new File(path);
return Uri.fromFile(file);
}
private void startGetPicFromCam() {
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri= getImgUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
startActivityForResult(intent,MEDIA_CAPTURE_RESULT_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if(requestCode==MEDIA_CAPTURE_RESULT_CODE){
if(resultCode==RESULT_OK){
try{
if(data.getData()!=null)
Toast.makeText(this,"saved to "+data.getData(),Toast.LENGTH_LONG).show();
else
Toast.makeText(this,"saved to path",Toast.LENGTH_LONG).show();
}
catch(Exception e){
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
}
}
}
}
Edit, it appears I didn't read your question close enough. It appears that the issue is that when you use EXTRA_OUTPUT, a null intent is passed back. If you want to get access to your data, just query the file that you passed in as an extra. See this and this question for more detailed information.