在应用启动时移除“从图库中选择图片”选项。 Android的

时间:2013-07-03 13:11:30

标签: android image android-intent android-activity media

我是android的新手。我尝试使用一些开源代码处理应用程序。 该应用程序使用图库中的图像,然后将其带入和imageview,我们对图像进行一些操作。

我需要的是,应用程序应该从一些默认图像开始已进入imageview ..然后我将添加一些按钮以便稍后从图库中选择图像。我无法理解这里的工作流程。如果有人可以指导我,那会很有意思。

这是我的代码主要活动。它还包含启动画面。

public class Main extends Activity {

static final String PREFS_FILE = "image_edit";

/** The URI of the Image to display. */


private int _wait;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main_screen);
    _wait = 1000;

    imageUri = null;
}

@Override
protected void onResume() {
    super.onResume();
    if (_wait != 0) {

        new CountDownTimer(_wait, _wait) {
            @Override
            public void onFinish() {
                if (imageUri != null) {
                    Intent viewActivity = new Intent(Main.this, Viewer.class);
                    viewActivity.putExtra("image", imageUri);
                    startActivity(viewActivity);
                } else {

                     startActivityForResult(newIntent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),0);
                }
            }

            @Override
            public void onTick(long millisUntilFinished) {

            }

        }.start();
        _wait = 0;
    } else {
        if (imageUri != null) {
            Intent viewActivity = new Intent(this, Viewer.class);
            viewActivity.putExtra("image", imageUri);
            startActivity(viewActivity);
        } else {

             startActivityForResult(newIntent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 0);
        }
    }
}

@Override
protected void onPause() {
    super.onPause();
    imageUri = null;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        imageUri = data.getData();
    } else {
        System.exit(0);
        Log.e("result", "BAD");
      }
   }
}

然后从这里拍摄的图像进入下一个名为Viewer活动的活动,该活动的onresume是这个

protected void onResume() {
    super.onResume();

    mHandler = new Handler();

    mPreferences = this.getSharedPreferences(Main.PREFS_FILE, 0);
    setContentView(R.layout.nothing);

    // Inflate all the views.
    int orientation = getResources().getConfiguration().orientation;
    basicInit(orientation);
    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        initPortrait();

    } else {
        initLandscape();
    }

    Intent intent = getIntent();
    Uri imageURI = null;
    Log.e("URI:", intent.getData() + "");
    if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_SEND)) {
        Bundle extras = intent.getExtras();
        if (extras.containsKey(Intent.EXTRA_STREAM)) {
            imageURI = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
        }
    } else if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_VIEW)) {
        imageURI = intent.getData();
    } else {
        imageURI = (Uri) intent.getParcelableExtra("image");
    }

    addImage(imageURI);

    addDraggableImages();

    _updateImageTask = new UpdateImage(mRelative, mHandler);


   }

所以我尝试在主视图中执行此操作

 if(wait !=0){}
    else{
    startActivity(new Intent(Main.this, Viewer.class));
    }

但这不起作用..错误是runtimeexception

所以,如果有人引导我,那将会有所帮助。谢谢

2 个答案:

答案 0 :(得分:0)

正如您在MainActivity的onResume()方法中看到的那样,首先它将转到else部分,即startActivityForResult,它将打开一个库并要求您选择一个图像。

您可以在main_screen.xml

中简单地创建一个按钮,而不是将其放入onResume中

然后在该按钮的onclickListener中实现此功能。

要默认查看图像,在xml的Imageview标签中可以输入:android:src =“@ drawable / imageFile”

对你有意义吗?

答案 1 :(得分:0)

下面给出了Java类和laoyout文件,类似于你正在讨论的应用程序。

MainActivity.java

      package com.tag.photocaptureandgallery;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.provider.MediaStore.MediaColumns;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
     import android.widget.ImageView;

     public class MainActivity extends Activity {

private final int SELECT_FILE = 1;
private final int REQUEST_CAMERA = 0;
private ImageView ivImage;
private Button btnSetImage;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ivImage = (ImageView) findViewById(R.id.ivImage);
    btnSetImage = (Button) findViewById(R.id.btnSelectPhoto);

    btnSetImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            selectImage();
        }
    });

}

private void selectImage() {
    final CharSequence[] items = { "Take Photo", "Choose from Library",
            "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(intent, REQUEST_CAMERA);
            } else if (items[item].equals("Choose from Library")) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            File f = new File(Environment.getExternalStorageDirectory()
                    .toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bm;
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();

                bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        btmapOptions);

                // bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
                ivImage.setImageBitmap(bm);

                String path = android.os.Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                f.delete();
                OutputStream fOut = null;
                File file = new File(path, String.valueOf(System
                        .currentTimeMillis()) + ".jpg");
                try {
                    fOut = new FileOutputStream(file);
                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == SELECT_FILE) {
            Uri selectedImageUri = data.getData();

            String tempPath = getPath(selectedImageUri, MainActivity.this);
            Bitmap bm;
            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
            ivImage.setImageBitmap(bm);
        }
    }
}

public String getPath(Uri uri, Activity activity) {
    String[] projection = { MediaColumns.DATA };
    Cursor cursor = activity
            .managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="5dp" >

    <Button
        android:id="@+id/btnSelectPhoto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Photo" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

</LinearLayout>

还为使用相机添加必要的权限。