I have the following code that pulls a pdf document from a remote viewer to view:
package com.example.techvault;
import java.io.File;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class PDFFromServerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_document);
Intent in= getIntent(); // gets the previously created intent
String url = in.getStringExtra("full_url");
if (!url.startsWith("http://") && !url.startsWith("https://")){
url = "http://ezdrawdocs.com" + url;
}
Log.d("pdf url: ",url);
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
File file = new File(folder, "Read.pdf");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
Downloader.DownloadFile(url, file);
showPdf();
}
public void showPdf()
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
}
When the backarrow is selected it redirects to a blank screen. How can I control which activity to redirect to when backarrow is selected?
Thanks
You can define that in your manifest under the activity with
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.yourapp.SomeActivity" />
What you're doing here is implementing what I call a "Passthrough Activity" (an Activity that has no UI and immediately launches a different Activity)... I personally try to avoid them.
That said, what you need to do is call Activity.finish() after showPDF() in Activity.onCreate(). That will close your passthrough so that when you back out of the PDF viewer you will not come back to the passthrough.
Related
hey i want to share my image in imageview to instagram and i using ACTION_SEND
before i want to share image i get my picture from other activity
i run app and i getting message "unable to download file"
and this is my code .... check my code if any error
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
public class editPhoto extends Activity {
String picturePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
overridePendingTransition(R.anim.push_left_in, R.anim.hold);
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
picturePath = getIntent().getStringExtra("selectedPhoto");
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
ImageView imageView = (ImageView) findViewById(R.id.iv_pic);
imageView.setImageBitmap(bitmap);
TextView tv = (TextView) findViewById(R.id.imagepath);
tv.setText(picturePath);
Button share = (Button) findViewById(R.id.btnShare);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(picturePath));
startActivity(Intent.createChooser(intent, "Share"));
}
});
}
}
put in onClickListener
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
startActivity(Intent.createChooser(share,"Share via"));
Hey I am new to android and eclipse environment.I've no idea of Java.I'm trying to create an app to open the camera of an android device.This is how my main activity.java looks like
package com.example.trycamera2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button_send);*****
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xray");
dir.mkdirs();
File file = new File(dir, "tmp_avatar_"
+ String.valueOf(System.currentTimeMillis()) + ".jpg");
fileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bitmap tempBitmap = (Bitmap) data.getExtras().get("data");
FileOutputStream out;
try {
out = new FileOutputStream(fileUri.getPath());
tempBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.v("ManageImage-other", "another phone type");
e.printStackTrace();
}
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
}
There is a red mark under button_send in the star marked line...it shows an alternative action_settings instead of button_send....but when I replace with action_settings and run the launch is cancelled.Failed to install .apk on emulator..any help will be of great use.
Sounds like Android can't find your button in any of the layouts. Double check your layouts to make sure they're there.
If they are, just go to the Project menu at the top, and select Clean. Select your project, then click Okay. It will take moment to clean and build, but that might fix your issue.
I'am uing the below code to open the android default gallery app. It opens all the image folders under sdcard. How can i open only one particular folder?
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Pick any photo"), SELECT_IMAGE_FROM_GALLERY_CONSTANT);
Use this following code to get a particular folder image.
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SDCARD123Activity extends Activity implements MediaScannerConnectionClient {
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE="image/*";
private MediaScannerConnection conn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File folder = new File("/sdcard/Photo/");
allFiles = folder.list();
// uriAllFiles= new Uri[allFiles.length];
for(int i = 0; i < allFiles.length; i++) {
Log.d("all file path" + i, allFiles[i]+allFiles.length);
}
// Uri uri= Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/Photo/"+allFiles[0];
System.out.println(" SCAN_PATH " +SCAN_PATH);
Log.d("SCAN PATH", "Scan Path " + SCAN_PATH);
Button scanBtn = (Button)findViewById(R.id.scanBtn);
scanBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
startScan();
}});
}
private void startScan() {
Log.d("Connected","success"+conn);
if (conn!=null) {
conn.disconnect();
}
conn = new MediaScannerConnection(this,this);
conn.connect();
}
#Override
public void onMediaScannerConnected() {
Log.d("onMediaScannerConnected","success"+conn);
conn.scanFile(SCAN_PATH, FILE_TYPE);
}
#Override
public void onScanCompleted(String path, Uri uri) {
try {
Log.d("onScanCompleted", uri + "success" + conn);
System.out.println("URI " + uri);
if (uri != null) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
} finally {
conn.disconnect();
conn = null;
}
}
}
I'd like to have a simple app that takes a picture and saves it in the Gallery, as shown here : http://developer.android.com/training/camera/photobasics.html
I tested the app on my device (2.1), i took the picture, they asked me to click on "ok", but the picture is not saved in the Gallery, do you know why? and how i could know where the error comes from? (the emulator does not have any "sd card", so i cannot really debug the project). Also, what's the difference between this technique and the one with getContentResolver().openOutputStream(uri); : http://developer.android.com/guide/topics/providers/content-providers.html#modifying ?
Also, i could try this solution : Picture saving in emulator but not on device but i was wondering why the code does not work on my device..?
package pack.one;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
public class TestPictureActivity extends Activity {
private static final String JPEG_FILE_SUFFIX = ".jpg";
Intent takePictureIntent;
String mCurrentPhotoPath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dispatchTakePictureIntent(11);
}
private void dispatchTakePictureIntent(int actionCode) {
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
try {
handleSmallCameraPhoto(data);
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleSmallCameraPhoto(Intent intent) throws IOException {
Bundle extras = intent.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.viewlayout, null);
ImageView mImageView = (ImageView) view.findViewById(R.id.V);
mImageView.setImageBitmap(mImageBitmap);
File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
galleryAddPic();
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(0));
String imageFileName = timeStamp + "_";
File image = File.createTempFile(
imageFileName,
JPEG_FILE_SUFFIX,
null //default location for temporary files
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
}
edit: in case someone wants to test the app, i just added these 2 lines in the manifest :
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
edit 2 : i actually have the picture in the "phone", when i plug the device to my computer, i can see a new picture file, with the right name, but it's empty, so i cannot open it. Plus, nothing appears in my gallery.
Thanks
Try addding this:
<uses-permission android:name="android.permission.CAMERA" />
Do you have this line in your AndroidManafest.xml file?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am trying reading a Pdf file from android app.
It builds an app and there is no error, but when I click the button it doesn't do anything. it looks like the app thinks there is no file.
I need a help because i am quite new to android apps but need to finish this job today or tomorrow. The person doing this is away at the moment.
package com.readPDF;
import java.io.File;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ReadPDF extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.pdfbutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "in.", Toast.LENGTH_LONG).show();
File file = new File("http://path/pathtopdf/mypdf.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(ReadPDF.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
private Context getContext() {
// TODO Auto-generated method stub
return null;
}
});
}
}
your pdf path is incorrect. change other valid path and then try again. then first try whether you set internet permission.
You can't directly fetch a file in a remote server using new File("http url").
URI uri = new URI("http", "//path/pathtopdf/mypdf.pdf", null);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(ReadPDF.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}