我是开发Android应用程序的新手。我正在尝试创建一个应用程序,允许您将图片保存在"图片"使用最后一个(相机)选项卡命名Property_Picture.jpg的文件夹。但是,最后两个选项卡都打开相机,一旦拍照,图片也不会被保存。任何帮助将不胜感激。谢谢!
这是我的代码:
package com.example.prcentry02;
import java.io.File;
import java.util.Locale;
import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v13.app.FragmentPagerAdapter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getFragmentManager();
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position== mSectionsPagerAdapter.getCount()-1)
{
return CameraFragment.newInstance(position);
}
else if (position== 0)
{
return LocationFragment.newInstance(position);
}
else
{
return propertyrecordFragment.newInstance(position);
}
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.PRC_Location).toUpperCase(l);
case 1:
return getString(R.string.PRC_1).toUpperCase(l);
case 2:
return getString(R.string.PRC_2).toUpperCase(l);
case 3:
return getString(R.string.Camera).toUpperCase(l);
}
return null;
}
}
public static class LocationFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static LocationFragment newInstance(int sectionNumber) {
LocationFragment fragment = new LocationFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public LocationFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_main, container,false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText("Location" + this.getArguments());
return rootView;
}
}
public static class CameraFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static CameraFragment newInstance(int sectionNumber) {
CameraFragment fragment = new CameraFragment();
Bundle args = new Bundle();
args.putInt(CameraFragment.ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public CameraFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.camera_main, container,false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText("camera" + ARG_SECTION_NUMBER);
Context context = getActivity();
PackageManager packageManager = context.getPackageManager();
if(packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) == false){
Toast.makeText(getActivity(), "This device does not have a camera.", Toast.LENGTH_SHORT)
.show();
}
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File("~/Pictures/", "Property_Picture.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent,0);
return rootView;
}
}
public static class propertyrecordFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static propertyrecordFragment newInstance(int sectionNumber) {
propertyrecordFragment fragment = new propertyrecordFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public propertyrecordFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.propertyrecordcard_main, container,false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText("PRC goes here");
return rootView;
}
}
}