如何从app小部件打开相机和图库的Intent选择器

时间:2016-08-19 12:01:40

标签: android android-intent

我创建了一个主屏幕小部件(1 * 1),我试图从该小部件打开相机和图库的意图选择器。我尝试从其他课程打开意图选择器,但它没有工作。以下是我的配置活动中的代码:

Intent clickIntent = new Intent(ConfigurationActivity.this, WidgetProviderSmall.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

PendingIntent pendingIntent = PendingIntent.getBroadcast(ConfigurationActivity.this, mAppWidgetId, clickIntent, 0);
views.setOnClickPendingIntent(R.id.img_widget, pendingIntent);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

这是来自我的AppWidgetProvider类:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction()==null) {
        Bundle extras = intent.getExtras();
        if(extras!=null) {              
            class.OpenIntentChooser();
        }
    }
    else {
        super.onReceive(context, intent);
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:-1)

对于相机代码,请根据需要使用

import android.app.Activity;  
import android.content.Intent;  
import android.graphics.Bitmap;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.widget.Button;  
import android.widget.ImageView;  

public class MainActivity extends Activity {  
     private static final int CAMERA_REQUEST = 1888;  
     ImageView imageView;  
     public void onCreate(Bundle savedInstanceState) {  

         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  

         imageView = (ImageView) this.findViewById(R.id.imageView1);  
         Button photoButton = (Button) this.findViewById(R.id.button1);  

         photoButton.setOnClickListener(new View.OnClickListener() {  

         @Override  
         public void onClick(View v) {  
              Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
              startActivityForResult(cameraIntent, CAMERA_REQUEST);  
         }  
        });  
       }  

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
      if (requestCode == CAMERA_REQUEST) {  
       Bitmap photo = (Bitmap) data.getExtras().get("data");  
       imageView.setImageBitmap(photo);  
      }  
   }  


}  

对于图库代码,请根据您的需要进行修改

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static int RESULT_LOAD_IMG = 1;
    String imgDecodableString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void loadImagefromGallery(View view) {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();
                ImageView imgView = (ImageView) findViewById(R.id.imgView);
                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(BitmapFactory
                        .decodeFile(imgDecodableString));

            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }

    }

}

在AndroidManifest.xml中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />